将从此 SQL 查询生成 DataTable
的行数最少的代码是什么?
SELECT *
FROM [Table1]
WHERE ([Date] BETWEEN @Date1 AND @Date2) AND
([Field1] IS NULL OR [Field2] IS NULL)
最佳答案
使用SqlDataAdapter填充数据表。
DataTable dt = new DataTable();
using (SqlConnection yourConnection = new SqlConnection("connectionstring"))
{
using (SqlCommand cmd = new SqlCommand("....your sql statement", yourConnection))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
}
}
使用using使用 SqlConnection
、SqlCommand
和 SqlDataAdapter
进行阻止,因为它们实现了 IDisposable
接口(interface)。也可以使用 Parameterized查询
关于c# - 从查询创建 DataTable 的最快方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12947872/