C# 错误必须声明标量变量“@EmpID”SQL 插入
C# Error Must declare the scalar variable "@EmpID" SQL Insert
我已经坚持这个问题好几个星期了,我已经在各个论坛上尝试解决其他人遇到的相同问题,但到目前为止它仍然行不通。我不确定我做错了什么。
我正在制作一个用户交互页面,该页面在单击按钮保存数据时获取数据并将其提交/插入到 SQL 数据库中。
EmpID 是来自生成用户员工 ID 的文本框的数据。然后将 EmpID 作为 EmployeeID 值存储在 SQL 数据库中。
当我 运行 它并尝试保存数据时,我一直收到错误;
"Must declare scalar variable "@EmpID
void UpdateInternal0()
{
try
{
using (SqlConnection con = new SqlConnection(Properties.Settings.Default.adminConnectionString))
{
con.Open();
foreach (DataRowView drv in InternalGrid0.SelectedItems)
{
DataRow row = drv.Row;
bool isSelected = Convert.ToBoolean(drv.Row[0]);
if (isSelected)
{
string sqlString = "INSERT INTO ChecklistTransitionTable (RelatedEmployeeID, RelatedDocIdx) " +
"VALUES (@EmpID, @DOC_Number)";
using (SqlCommand cmd = new SqlCommand(sqlString, con))
{
for (int i = 0; i < InternalGrid0.Columns.Count; i++)
{
int DocIdx = (int)drv.Row[i];
int EmployeeID = Convert.ToInt32(EmpID.Text);
cmd.Parameters.AddWithValue("@EmpID", EmployeeID);
cmd.Parameters.AddWithValue("@DOC_Number", DocIdx);
cmd.ExecuteScalar(); cmd.Parameters.Clear();
int result = cmd.ExecuteNonQuery();
Console.WriteLine("Test");
//Check Error
if (result < 0)
{
System.Windows.Forms.MessageBox.Show("Error inserting data into database!");
}
}
}
}
}
}
System.Windows.Forms.MessageBox.Show("Employee data successfully updated.");
}
catch (SqlException ex)
{
System.Windows.Forms.MessageBox.Show(string.Format("\nMessage ---\n{0}", ex.Message));
}
}
private void BtnGridUpdate_Click(object sender, EventArgs e)
{
UpdateInternal0();
}
您正在执行查询两次。
第一次使用 ExecuteScalar()
,然后在 ExecuteNonQuery()
调用中清除参数值并重新执行查询(现在没有参数值)。后者抛出异常
也许这个修改后的版本会按预期工作。
void UpdateInternal0(int employeeId)
{
try
{
using (SqlConnection con = new SqlConnection(Properties.Settings.Default.adminConnectionString))
{
con.Open();
foreach (DataRowView drv in InternalGrid0.SelectedItems)
{
DataRow row = drv.Row;
bool isSelected = Convert.ToBoolean(drv.Row[0]);
if (isSelected)
{
string sqlString = "INSERT INTO ChecklistTransitionTable (RelatedEmployeeID, RelatedDocIdx) VALUES (@EmpID, @DOC_Number)";
using (SqlCommand cmd = new SqlCommand(sqlString, con))
{
for (int i = 0; i < InternalGrid0.Columns.Count; i++)
{
int DocIdx = 0;
try {
DocIdx = (int)drv.Row[i];
} catch (InvalidCastException ice) {
// You should handle this here...
}
cmd.Parameters.AddWithValue("@EmpID", employeeId);
cmd.Parameters.AddWithValue("@DOC_Number", DocIdx);
int result = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
Console.WriteLine("Test");
//Check Error
if (result < 0)
{
System.Windows.Forms.MessageBox.Show("Error inserting data into database!");
}
}
}
}
}
}
System.Windows.Forms.MessageBox.Show("Employee data successfully updated.");
}
catch (SqlException ex)
{
System.Windows.Forms.MessageBox.Show(string.Format("\nMessage ---\n{0}", ex.Message));
}
}
private void BtnGridUpdate_Click(object sender, EventArgs e)
{
int employeeID = Convert.ToInt32(EmpID.Text);
UpdateInternal0(employeeID);
}
我已经坚持这个问题好几个星期了,我已经在各个论坛上尝试解决其他人遇到的相同问题,但到目前为止它仍然行不通。我不确定我做错了什么。
我正在制作一个用户交互页面,该页面在单击按钮保存数据时获取数据并将其提交/插入到 SQL 数据库中。
EmpID 是来自生成用户员工 ID 的文本框的数据。然后将 EmpID 作为 EmployeeID 值存储在 SQL 数据库中。
当我 运行 它并尝试保存数据时,我一直收到错误;
"Must declare scalar variable "@EmpID
void UpdateInternal0()
{
try
{
using (SqlConnection con = new SqlConnection(Properties.Settings.Default.adminConnectionString))
{
con.Open();
foreach (DataRowView drv in InternalGrid0.SelectedItems)
{
DataRow row = drv.Row;
bool isSelected = Convert.ToBoolean(drv.Row[0]);
if (isSelected)
{
string sqlString = "INSERT INTO ChecklistTransitionTable (RelatedEmployeeID, RelatedDocIdx) " +
"VALUES (@EmpID, @DOC_Number)";
using (SqlCommand cmd = new SqlCommand(sqlString, con))
{
for (int i = 0; i < InternalGrid0.Columns.Count; i++)
{
int DocIdx = (int)drv.Row[i];
int EmployeeID = Convert.ToInt32(EmpID.Text);
cmd.Parameters.AddWithValue("@EmpID", EmployeeID);
cmd.Parameters.AddWithValue("@DOC_Number", DocIdx);
cmd.ExecuteScalar(); cmd.Parameters.Clear();
int result = cmd.ExecuteNonQuery();
Console.WriteLine("Test");
//Check Error
if (result < 0)
{
System.Windows.Forms.MessageBox.Show("Error inserting data into database!");
}
}
}
}
}
}
System.Windows.Forms.MessageBox.Show("Employee data successfully updated.");
}
catch (SqlException ex)
{
System.Windows.Forms.MessageBox.Show(string.Format("\nMessage ---\n{0}", ex.Message));
}
}
private void BtnGridUpdate_Click(object sender, EventArgs e)
{
UpdateInternal0();
}
您正在执行查询两次。
第一次使用 ExecuteScalar()
,然后在 ExecuteNonQuery()
调用中清除参数值并重新执行查询(现在没有参数值)。后者抛出异常
也许这个修改后的版本会按预期工作。
void UpdateInternal0(int employeeId)
{
try
{
using (SqlConnection con = new SqlConnection(Properties.Settings.Default.adminConnectionString))
{
con.Open();
foreach (DataRowView drv in InternalGrid0.SelectedItems)
{
DataRow row = drv.Row;
bool isSelected = Convert.ToBoolean(drv.Row[0]);
if (isSelected)
{
string sqlString = "INSERT INTO ChecklistTransitionTable (RelatedEmployeeID, RelatedDocIdx) VALUES (@EmpID, @DOC_Number)";
using (SqlCommand cmd = new SqlCommand(sqlString, con))
{
for (int i = 0; i < InternalGrid0.Columns.Count; i++)
{
int DocIdx = 0;
try {
DocIdx = (int)drv.Row[i];
} catch (InvalidCastException ice) {
// You should handle this here...
}
cmd.Parameters.AddWithValue("@EmpID", employeeId);
cmd.Parameters.AddWithValue("@DOC_Number", DocIdx);
int result = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
Console.WriteLine("Test");
//Check Error
if (result < 0)
{
System.Windows.Forms.MessageBox.Show("Error inserting data into database!");
}
}
}
}
}
}
System.Windows.Forms.MessageBox.Show("Employee data successfully updated.");
}
catch (SqlException ex)
{
System.Windows.Forms.MessageBox.Show(string.Format("\nMessage ---\n{0}", ex.Message));
}
}
private void BtnGridUpdate_Click(object sender, EventArgs e)
{
int employeeID = Convert.ToInt32(EmpID.Text);
UpdateInternal0(employeeID);
}