开发者

Problems with adding query parameters

开发者 https://www.devze.com 2023-02-16 15:10 出处:网络
Code: com.CommandText = \"select username, pass from Employees where lastName like \'@Last\' and firstName like \'@First\'\";

Code:

com.CommandText = "select username, pass from Employees where lastName like '@Last' and firstName like '@First'";
com.Parameters.AddWithValue("@Last", lastName); // lastName i开发者_如何学Pythons a method argument

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

No matter what I do, the parameters are not inserted. What gives?


Do not put single quotes around your parameters, these are implied anyway.

Also, try using SqlCommand.CreateParameter as well:

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);


try to change the '@Last' to @Last, same thing for First...

0

精彩评论

暂无评论...
验证码 换一张
取 消