有没有比DataTable.Load(DataReader)填充数据表更快/更智能的方
我正在做一些研究,以便更好地理解SQL并使用DataTables.
所以我试图从MS SQL数据库中获得良好的性能读取数据并将其加载到datagridview中. 我已经创建了一个SQL函数,我从我的工具调用它并将结果加载到数据表中. 到目前为止我所做的是: private DataTable GetDataFromDB(string userId,string docId,DateTimeOffset date) { string cmd = String.Format("select * from dbo.GetData(@userId,@docId,@dueDate);"); using (SqlConnection conn = new SqlConnection(connectionString)) { if (conn.State != ConnectionState.Open) conn.Open(); SqlCommand command = new SqlCommand(cmd,conn); if (String.IsNullOrEmpty(userId)) command.Parameters.AddWithValue("@userId",DBNull.Value); else command.Parameters.AddWithValue("@userId",userId); if (String.IsNullOrEmpty(docId)) command.Parameters.AddWithValue("@docId",DBNull.Value); else command.Parameters.AddWithValue("@docId",docId); command.Parameters.AddWithValue("@dueDate",dueDate); SqlDataReader reader = command.ExecuteReader(); stopWatch.Reset(); stopWatch.Start(); table.BeginLoadData(); table.Load(reader,LoadOption.Upsert); table.EndLoadData(); stopWatch.Stop(); reader.Close(); reader.Dispose(); conn.Close(); } return table; } 我已经做了一些谷歌研究,这是我能想到的最好的.到目前为止它运作良好,但我很好奇是否有可能更快地获得结果.有任何想法吗? 非常感谢! 解决方法
性能将根据您加载的数据量和数量而有所不同.
下面的链接将为您提供性能报告的解决方案.用你的方法尝试这样的东西,并比较性能. 希望这会帮助你. Fastest way to populate datatable (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |