1

刚接触 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();
    }
}
4

2 回答 2

2

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

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

提示:

  • 任务完成后或出现错误时始终关闭连接。
于 2020-10-05T13:59:39.107 回答
0

感谢所有在这里插话的人。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();
    }

}
于 2020-10-05T14:37:41.770 回答