加速c#中的ExecuteReader
本文关键字:ExecuteReader 中的 加速 | 更新日期: 2023-09-27 18:03:23
我在许多循环中使用c# SqlDataReader
。不幸的是,我不能读取整个表并将数据存储在列表中。所以我要一次又一次地生成一个SqlDataReader
。一旦SqlDataReader
被创建,它非常快。但是通过ExecuteReader
创建SqlDataReader
需要花费太多时间。
是否有可能提高SqlDataReader
的创建时间?
我正在使用。net 4.5.1和SQL Server 2008。
string sql = "select CURRENT_TIMESTAMP";
var connection = Connections.Get();
SqlCommand sqlCommand = new SqlCommand(sql, connection);
sqlCommand.CommandTimeout = 0;
var reader = sqlCommand.ExecuteReader(CommandBehavior.Default);
谢谢迈克尔。
string conString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();
SqlConnection conn = new SqlConnection(conString);
conn.Open();
SqlCommand cmd = new SqlCommand("select TOP 50000 * from users", conn);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
请使用DataTable
和SqlDataAdapter
读取数据而不是使用ExecuteReader