C# 异常的SQL/数据问题

C# 异常的SQL/数据问题,c#,asp.net,sql,stored-procedures,C#,Asp.net,Sql,Stored Procedures,我们有一个报告给了我们一些严重的问题,所以我决定把它放到一个控制台应用程序中,以便解决这些问题 该报告只是从SQL中进行简单的单选,返回大约25列, 我们的日期范围可以是3-6个月,返回大约10k行,所以我们不讨论太多的数据 下面是正在发生的事情,当报告运行时,它正在从我们的网站超时,在控制台中,它需要13-18分钟才能完成,等待似乎发生在da.Fill(ds) da.Fill(ds); 现在有一件奇怪的事情,它在SQLServerManagementStudio中运行大约1-3秒,当我们的De

我们有一个报告给了我们一些严重的问题,所以我决定把它放到一个控制台应用程序中,以便解决这些问题

该报告只是从SQL中进行简单的单选,返回大约25列, 我们的日期范围可以是3-6个月,返回大约10k行,所以我们不讨论太多的数据

下面是正在发生的事情,当报告运行时,它正在从我们的网站超时,在控制台中,它需要13-18分钟才能完成,等待似乎发生在da.Fill(ds)

da.Fill(ds); 现在有一件奇怪的事情,它在SQLServerManagementStudio中运行大约1-3秒,当我们的Delphi开发人员创建类似的应用程序时,运行也需要几秒钟,这只在使用.NET时发生

我们尝试将数据集更改为加载到datareader中, 使用此代码。。 using (var dr = _command.ExecuteReader()) { if (dr.HasRows) { int i = 0; while (dr.Read()) { var startRead = DateTime.Now; Console.Write("{2}\t{0}\t{1}\t", dr.GetInt32(0), dr.GetString(1), i); var tookRead = DateTime.Now.Subtract(startRead); Console.WriteLine("Took: " + tookRead); i++; } } 使用(var dr=_command.ExecuteReader()) { 如果(哈斯罗博士) { int i=0; while(dr.Read()) { var startRead=DateTime.Now; Write(“{2}\t{0}\t{1}\t”,dr.GetInt32(0),dr.GetString(1),i); var tookRead=DateTime.Now.Subtract(startRead); Console.WriteLine(“take:+tookRead”); i++; } } 但是它一点帮助都没有,它只是在Chuck中显示,但经常有延迟。我在考虑它的SQL,但无法解释为什么它在Delphi和SQL Management Studio中工作良好

我试过在所有框架上使用.NET2.0、3.5和4

这是我的密码

public static DataSet GetData() { var now = DateTime.Now; var _command = new SqlCommand(); var _connection = new SqlConnection(); try { _connection.ConnectionString = connectionString; _command.Connection = _connection; _command.CommandText = storedProcedure; _command.CommandType = CommandType.StoredProcedure; _command.CommandTimeout = 60; if (string.IsNullOrEmpty(_connection.ConnectionString)) { throw new Exception("Connection String was not supplied"); } _command.Parameters.Add(new SqlParameter("DateFrom", dateFrom)); _command.Parameters.Add(new SqlParameter("DateTo", dateTo)); SqlDataAdapter da; var ds = new DataSet(); _connection.Open(); var done = DateTime.Now; da = new SqlDataAdapter(_command); da.Fill(ds); if (ds == null) { throw new Exception("DataSet is null."); } if (ds.Tables.Count == 0) { throw new Exception("Table count is 0"); } var took = done.Subtract(now); return ds; } catch (Exception ex) { File.WriteAllText(Path.Combine(Application.StartupPath, String.Format("Exception{0:MMddyyyy_HHmmss}.log", DateTime.Now)), ex.ToString()); } finally { if (_connection.State != ConnectionState.Closed) { _connection.Close(); } } return null; } 公共静态数据集GetData() { var now=DateTime.now; var_command=new SqlCommand(); var_connection=new SqlConnection(); 尝试 { _connection.ConnectionString=ConnectionString; _command.Connection=\u Connection; _command.CommandText=存储过程; _command.CommandType=CommandType.storedProcess; _command.CommandTimeout=60; if(string.IsNullOrEmpty(_connection.ConnectionString)){抛出新异常(“未提供连接字符串”);} _Add(新的SqlParameter(“DateFrom”,DateFrom)); _Add(新的SqlParameter(“DateTo”,DateTo)); sqldatada; var ds=新数据集(); _connection.Open(); var done=DateTime.Now; da=新的SqlDataAdapter(_命令); da.填充(ds); 如果(ds==null){抛出新异常(“数据集为null”);} 如果(ds.Tables.Count==0){抛出新异常(“表计数为0”);} var take=完成。减去(现在); 返回ds; } 捕获(例外情况除外) { File.WriteAllText(Path.Combine(Application.StartupPath,String.Format(“Exception{0:MMddyyyy\u HHmmss}.log”,DateTime.Now)),例如ToString(); } 最后 { 如果(_connection.State!=ConnectionState.Closed){u connection.Close();} } 返回null; }
有什么想法吗?我们的DBA在责怪框架,我实际上在责怪SQL中的某些东西(可能是统计数据或损坏的db)

您提供的信息没有包含足够的细节来真正帮助

如果SSMS真的那么快,那么原因可能是某些会话/连接设置-SSMS使用的设置与.NET相比有细微的不同


有关什么可能不同/错误等的一些解释和提示,请参见.NET和其他客户端(SQL Management Studio)之间SQL性能的差异通常是由于连接配置不同所致-常见的罪魁祸首是ANSI_null;ANSI_PADDING


请尝试查看SQL Management Studio中如何配置连接,然后在.NET应用程序中复制相同的内容。

如果查询在SSM中正常运行,您认为这是SQL吗?执行SQL语句后,您已向下滚动到SQL Management Studio中的最后一行了吗?请查看此线程:就像侧面n一样注意,我认为
var done=DateTime.Now;
行应该在
finally
子句中移动。现在它没有对最重要的事情进行计时-
da.Fill(ds);