开发者

SQLParameter not working properly

开发者 https://www.devze.com 2022-12-25 09:42 出处:网络
I am trying to access a stored procedure a开发者_开发百科nd I\'m getting an error that says: Procedure or function \'getbug\' expects parameter \'@bugID\', which was not supplied.

I am trying to access a stored procedure a开发者_开发百科nd I'm getting an error that says:

Procedure or function 'getbug' expects parameter '@bugID', which was not supplied.

This is my code to call the procedure.

SqlCommand cmd = new SqlCommand("getbug", cn);
cmd.Parameters.Add(new SqlParameter("bugID", bugID));

bugID is set as 1089 (and is type int)

I can't figure out why this won't work.


Try this instead

SqlCommand cmd = new SqlCommand("getbug", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@bugID", bugID));


Try adding the "@" to the parameter

SqlCommand cmd = new SqlCommand("getbug", cn);
cmd.Parameters.Add(new SqlParameter("@bugID", bugID));


More code will help. Here's a couple of ideas though.

If you're calling a stored procedure, you need to specify the CommandType. Also, you can use the AddWithValue method to shorten your code.

using (var cn = new SqlConnection("your connection string"))
{
    using (var cmd = new SqlCommand("getbug", cn))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@bugID", bugID);

        //etc...
    }
}
0

精彩评论

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