2011-03-10 78 views
0

代码:与添加查询参数的问题

com.CommandText = "select username, pass from Employees where lastName like '@Last' and firstName like '@First'"; 
com.Parameters.AddWithValue("@Last", lastName); // lastName is a method argument 

SqlParameter param = new SqlParameter(); 
param.ParameterName = "@First"; 
param.Value = firstName; 
com.Parameters.Add(param); 

不管我做什么,参数没有插入。是什么赋予了?

+2

会发生什么,你得到一个例外?如果是这样,什么是例外? – CodingGorilla 2011-03-10 19:11:45

+0

正如我所说的,参数没有插入;该字符串具有文字@Last和@First。 – 2011-03-10 19:17:04

+0

您是否使用Sql profiler查看发送到服务器的命令?你如何确定“该字符串的文字是@Last和@First”?这是什么意思? – 2011-03-10 20:46:37

回答

4

不要把单引号放在你的参数中,这些都是隐含的。

而且,尝试使用SqlCommand.CreateParameter还有:

com.CommandText = "select username, pass from Employees where lastName like @Last and firstName like @First"; 
com.Parameters.AddWithValue("@Last", lastName); // lastName is a method argument 

SqlParameter param = com.CreateParameter(); 
param.ParameterName = "@First"; 
param.Value = firstName; 
com.Parameters.Add(param); 
2

试图改变 '@Last' 到@Last,对于第一次同样的事情...

+0

之前试过了,还是没有工作。 – 2011-03-10 19:15:41

+0

你如何执行命令? – THEn 2011-03-10 19:53:11

+0

myDataAdapter.SelectCommand.Parameters.Add(“@ Last”,SqlDbType.VarChar,80).Value = firstName – THEn 2011-03-10 20:02:11