ExecutenonQuery not working

冷暖自知 提交于 2019-12-07 09:57:07

问题


I have stored proc as below:

ALTER PROC pr_Update_Users_Nomination
(
      @UserID AS VARCHAR(100),  
      @Nominated AS BIT
)
AS

UPDATE User
SET isNominated = @Nominated
WHERE 
EMPID = @UserID;

I want to call this procedure from c# code: Below is the code I am trying:

void OpenConnection()
{
string Nominated = "False";
    //Connection String
        string sConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConString1"].ConnectionString;
        SqlConnection mySqlCon = new SqlConnection(sConnString);
        SqlCommand mySqlCom = mySqlCon.CreateCommand();

        //Call the stored proc and provide in parameters
        mySqlCom.CommandText = "EXECUTE pr_Update @UserID @Nominated";
        mySqlCom.Parameters.Add("@UserID", SqlDbType.VarChar, 20).Value = UserID;
        mySqlCom.Parameters.Add("@Nominated", SqlDbType.Bit).Value = Nominated;

        mySqlCon.Open();
        mySqlCom.ExecuteNonQuery();
        mySqlCon.Close();
}

I get an error saying

Incorrect Syntax near @Nominated

回答1:


first, when executing a procedure with parameter(s), separate the parameters with a comma

EXECUTE pr_Update @UserID, @Nominated

second, modify your code into this,

    string sConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConString1"].ConnectionString;
    using(SqlConnection mySqlCon = new SqlConnection(sConnString))
    {
        using(SqlCommand mySqlCom = new SqlCommand())
        {
            mySqlCom.Connection = mySqlCon;
            mySqlCom.CommandText = "pr_Update";
            mySqlCom.CommandType = CommandType.StoredProcedure;
            mySqlCom.Parameters.Add("@UserID", SqlDbType.VarChar, 20).Value = UserID;
            mySqlCom.Parameters.Add("@Nominated", SqlDbType.Bit).Value = Nominated;
            try
            {
                mySqlCon.Open();
                mySqlCom.ExecuteNonQuery();
            }
            catch(SqlException ex)
            {
                // do something with the exception
                // don't hide it
            }

        }
    }



回答2:


You are missing a comma (,) between the parameters.

It should be

mySqlCom.CommandText = "EXECUTE pr_Update @UserID, @Nominated";
mySqlCom.Parameters.Add("@UserID", SqlDbType.VarChar, 20).Value = UserID;
mySqlCom.Parameters.Add("@Nominated", SqlDbType.Bit).Value = Nominated;

Alternatively, since all you are doing is calling a stored proc, you could do:

mySqlCom.CommandType = CommandType.StoredProcedure ;
mySqlCom.CommandText = "pr_Update"; //no need to specify parameter names
mySqlCom.Parameters.Add("@UserID", SqlDbType.VarChar, 20).Value = UserID;
mySqlCom.Parameters.Add("@Nominated", SqlDbType.Bit).Value = Nominated;



回答3:


Give only name of stored procedure, as you are adding parameter in statements after this. Also set CommandType.

 mySqlCom.CommandText = "pr_Update";
 mySqlCom.CommandType = CommandType.StoredProcedure;



回答4:


You are invoking wrong SQL. You should set the command text of command to pr_Update only:

mySqlCom.CommandText = "pr_Update";

And set type command type to stored procedure:

mySqlCom.CommandType = CommandType.StoredProcedure;

See MSDN page for more.



来源:https://stackoverflow.com/questions/13381318/executenonquery-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!