C# SqlDataAdapter 必须声明标量变量 Sql 异常

C# SqlDataAdapter Must declare the scalar variable Sql Exception

C# 新手,正在开发 Windows 表单应用程序。我正在尝试对 SQL 数据库执行更新查询,但将 运行 保留为“必须声明标量变量”错误,我不明白为什么。

下面的代码成功打开了连接。我的更新声明有效。浏览了很多关于这个主题的帖子,我只是没有看到我的错误...任何帮助将不胜感激。

public void SetJobStatus(long JobId)
{
    string strSql = "update Jobmaster set jobstatus = 5 where equid = @stationId AND ID <> @jobId AND OfflineEntry = 0;";

    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = GlobalVars.connString;
        conn.Open();
        // use the connection here, and check to confirm it is open
        if (conn.State != ConnectionState.Open)
        {
            if (conn != null)
            {
                conn.Close();
            }
            conn.Open();
        }
        SqlCommand command;
        SqlDataAdapter adapter = new SqlDataAdapter();

        command = new SqlCommand(strSql, conn);
        //below AddWithValue gives error:
        //System.Data.SqlClient.SqlException: 'Must declare the scalar variable "@stationId".'
        //command.Parameters.AddWithValue("@stationId", 1);
        //command.Parameters.AddWithValue("@jobId", JobId);
        
        //next I tried this, and the same error:
        //System.Data.SqlClient.SqlException: 'Must declare the scalar variable "@stationId".'
        command.Parameters.Add("@stationId", SqlDbType.Int);
        command.Parameters["@stationId"].Value = 1;
        command.Parameters.Add("@jobId", SqlDbType.Int);
        command.Parameters["@jobId"].Value = JobId;

        adapter.UpdateCommand = new SqlCommand(strSql, conn);
        adapter.UpdateCommand.ExecuteNonQuery();
    }
}

我已经检查了您的代码,需要进行一些更改。请尝试 运行 下面的代码:

public void SetJobStatus(int JobId)
{
    string strSql = "update Jobmaster set jobstatus = 5 where equid = @stationId AND ID <> @jobId AND OfflineEntry = 0;";

    using (SqlConnection conn = new SqlConnection())
    {
        try
        {
            conn.ConnectionString = GlobalVars.connString;
            conn.Open();
            SqlCommand command = new SqlCommand(strSql, conn);
            command.CommandType = CommandType.Text;
            command.Parameters.Add("@stationId", SqlDbType.Int);
            command.Parameters["@stationId"].Value = 1;
            command.Parameters.Add("@jobId", SqlDbType.Int);
            command.Parameters["@jobId"].Value = JobId;
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }
        finally
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }
    }
}

提示:

  • 总是在任务完成后或出现错误时关闭连接。

感谢所有在这里插话的人。 WSC 的评论起到了改变 adapter.UpdateCommand = command; 的作用。在进行 WSC 更改后,我尝试了三种添加参数的变体 - 其中两种有效,一种无效。

我修改后的代码如下。我在代码中列出了所有三种变体 - 希望这会帮助其他人。

public void SetJobStatus(long JobId)
{
    string strSql = "update Jobmaster set jobstatus = 5 where equid = @stationId AND ID <> @jobId AND OfflineEntry = 0;";

    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = GlobalVars.connString;
        conn.Open();
        // use the connection here, and check to confirm it is open
        if (conn.State != ConnectionState.Open)
        {
            if (conn != null)
            {
                conn.Close();
            }
            conn.Open();
        }
        SqlCommand command;
        SqlDataAdapter adapter = new SqlDataAdapter();

        command = new SqlCommand(strSql, conn);
        
        //works
        command.Parameters.AddWithValue("@stationId", GlobalVars.stationId);
        command.Parameters.AddWithValue("@jobId", JobId);

        //works
        //command.Parameters.Add("@stationId", SqlDbType.Int);
        //command.Parameters["@stationId"].Value = 5;
        //command.Parameters.Add("@jobId", SqlDbType.Int);
        //command.Parameters["@jobId"].Value = JobId;

        //throws error at adapter.UpdateCommand.ExecuteNonQuery line:
        //'The parameterized query '(@stationId int,@jobId int)update Jobmaster set jobstatus = 5 wh' expects the parameter '@stationId', which was not supplied.'
        //command.Parameters.Add("@stationId", SqlDbType.Int, 5);
        //command.Parameters.Add("@jobId", SqlDbType.Int, (int)JobId);

        adapter.UpdateCommand = command;
        adapter.UpdateCommand.ExecuteNonQuery();
    }

}