我正在使用C#函数根据SqlConnection
,SqlCommand
和SqlDataAdapter
查询数据库,但是当我尝试检索大量行时,性能非常差(很少)数以百万计)。
using (SqlConnection mySqlConnection = new SqlConnection(connectionString))
using (SqlCommand mySqlCommand = new SqlCommand(query, mySqlConnection)){
mySqlConnection.Open();
DataSet myDataSet = new DataSet();
using (SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(mySqlCommand)) {
affectedRow = mySqlDataAdapter.Fill(myDataSet);
}
}
有没有办法优化大数据集的查询?
正如比较一下,这段VBA代码只需要4-5而不是20-22秒的C#
Set cnt = New ADODB.Connection
Set rst = New ADODB.Recordset
Set cmd = New ADODB.Command
cnt.Open
Set rst = cnt.Execute(queryString)
Dim nDimArray As Variant
nDimArray = rst.GetRows
cnt.Close
答案 0 :(得分:6)
不要使用DataSet。使用类似于您在VBA中所做的SqlReader。
using (var mySqlConnection = new SqlConnection(connectionString))
using (var mySqlCommand = new SqlCommand(query, mySqlConnection))
{
mySqlConnection.Open();
var reader = mySqlCommand.ExecuteReader();
while (reader.Read())
{
}
reader.Close();
}
此处提供更多信息:https://msdn.microsoft.com/en-us/library/9kcbe65k%28v=vs.110%29.aspx
你也应该能够这样做,除非你在阅读过程中抛出异常,否则将自行清理:
using (var mySqlConnection = new SqlConnection(connectionString))
{
using (var mySqlCommand = new SqlCommand(query, mySqlConnection))
{
mySqlConnection.Open();
using (var reader = mySqlCommand.ExecuteReader())
{
while (reader.Read())
{
}
}
}
}