Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL脚本需要很长时间才能运行_C#_Sql_Enterprise Library - Fatal编程技术网

C# SQL脚本需要很长时间才能运行

C# SQL脚本需要很长时间才能运行,c#,sql,enterprise-library,C#,Sql,Enterprise Library,我目前正在使用Microsoft Enterprise Library Data Access 5.0执行存储过程 Database myDatabase = DatabaseFactory.CreateDatabase(); using (DbCommand command = myDatabase.GetStoredProcCommand("myStoredProc")) { //Add parameters here using (IDataReader dataReader

我目前正在使用Microsoft Enterprise Library Data Access 5.0执行存储过程

Database myDatabase = DatabaseFactory.CreateDatabase();
using (DbCommand command = myDatabase.GetStoredProcCommand("myStoredProc"))
{
   //Add parameters here
   using (IDataReader dataReader = myDatabase.ExecuteReader(command))
   {
      while (dataReader.Read())
      {
      }
   }
}
一切正常,但运行需要很长时间。当我打开SQL Profiler时,我可以看到存储过程运行大约需要50秒。但是,如果我从探查器中获取相同的脚本并在SQLManagementStudio中运行它,那么返回所有行只需要大约480毫秒


有人碰到过这个问题吗?为什么会有很大的不同呢?

很容易做到。

尝试将字符串参数类型更改为
DbType.ansisting
,看看是否有帮助。
问题可能是,如果数据库列是varchar(…),并且具有索引,并且与该列匹配的参数是nvarchar(…),那么SQL Server将忽略该索引并执行表扫描。
解决方案是强制参数键入ansisting,以便SQL Server使用索引。

如果删除while循环,它会改变吗?我必须试试看。这个问题每周在MSDN SQL论坛上出现几次;这里也一定是复制品。我忘记了确切的原因,但这是一个需要调整的默认设置。您的参数之一是字符串吗?这可能是声名狼藉的nvarchar参数对varchar索引的问题(或者是另一种方法?)是的,参数是字符串varchar(150)。并这样添加:myDatabase.AddInParameter(命令“@Param”,DbType.String,value);是的,这是另一个很好的原因;不是我想的那个。