简体   繁体   English

参数在DBCommand中添加导致超时

[英]Parameter Add Causing Timeout in DBCommand

I have a Parameter add function like bellow: 我有一个像bellow一样的参数添加功能:

public static void AddParameter(DbCommand comm, string ParamName, Object objValue, DbType Paramtype,int paramSize)
{
      //comm.Parameters.Clear();
      DbParameter param = comm.CreateParameter();
      param.ParameterName = ParamName;
      param.DbType = Paramtype;
      param.Size = paramSize;
      param.Direction = ParameterDirection.Input;
      param.Value = objValue;
      comm.Parameters.Add(param);
}

Now when do the following: 现在何时执行以下操作:

conn.Open();
DbCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM tableName WHERE MemberID=@MemberID";
AddParameter(cmd, "@MemberID", "00000970-b1fd-49ad-89fc-1de066c0076c", DbType.String, 50);
cmd.ExecuteReader();

I get Timeout exception. 我得到超时异常。 But if I run the same command Like 但是,如果我运行相同的命令喜欢

SELECT * FROM tableName WHERE MemberID='00000970-b1fd-49ad-89fc-1de066c0076c'

I get my desired output... Can anybody help me find out where is my problem? 我得到了我想要的输出......任何人都可以帮我找出问题所在? conn here is a SqlConnection. conn这里是一个SqlConnection。 Thanks. 谢谢。

You state that the column is varchar(50) , but you are using DbType.String ; 您声明该列是varchar(50) ,但您使用的是DbType.String ; however, DbType.String is nvarchar(...) - ie unicode. 但是, DbType.Stringnvarchar(...) - 即unicode。 Now, sometimes the server will do the conversion the "right way around" (where "right" here means: the one that doesn't kill performance), but sometimes it will make the alternative choice (for example, it might decide that it can't use the index and do a table scan, or even worse - it might decide to convert every row in turn for the comparison). 现在,有时服务器会以“正确的方式”进行转换(这里的“右”意味着:不会破坏性能的转换),但有时它会做出替代选择(例如,它可能会决定它不能使用索引并进行表扫描,甚至更糟 - 它可能决定依次转换每一行进行比较)。 The best thing to do, then, is to specify the correct data-type in the first place: by using DbType.AnsiString . 那么,最好的办法是首先指定正确的数据类型:使用DbType.AnsiString There's nothing "wrong" with either DbType.String or DbType.AnsiString - so long as they are correctly being used to represent nvarchar and varchar respectively. DbType.StringDbType.AnsiString没有任何“错误” - 只要它们分别正确地用于表示nvarcharvarchar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM