I want to update my databases tables, but I get this error
我想更新我的数据库表,但是我收到了这个错误
System.Data.SqlClient.SqlException (0x80131904): The variable name '@ssn' has already been declared. Variable names must be unique within a query batch or stored procedure. Must declare the scalar variable "@Name".
System.Data.SqlClient.SqlException(0x80131904):已声明变量名'@ssn'。变量名在查询批处理或存储过程中必须是唯一的。必须声明标量变量“@Name”。
Code:
private void button3_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=HP\BILSAY;Initial Catalog=Railway System;Integrated Security=True");
conn.Open();
try
{
SqlCommand cmd = new SqlCommand("UPDATE Employe SET SSN=@ssn,Name=@Name,Surname=@Surname,Address=@Address,Age=@Age,Phone=@Phone,Profession=@Profession WHERE SSN=@SSN ",conn);
cmd.Parameters.AddWithValue("@ssn", dataGridView1.CurrentRow.Cells[0].Value);
cmd.Parameters.AddWithValue("@ssn", textBox2.Text);
cmd.Parameters.AddWithValue("@Name", textBox3.Text);
cmd.Parameters.AddWithValue("@Surname", textBox4.Text);
cmd.Parameters.AddWithValue("@Adress", textBox5.Text);
cmd.Parameters.AddWithValue("@Age", textBox6.Text);
cmd.Parameters.AddWithValue("@Phone", textBox7.Text);
cmd.Parameters.AddWithValue("@Profesion", textBox8.Text);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.ExecuteNonQuery();
conn.Close();
Employe emp = new Employe();
emp.Show();
MessageBox.Show("Updated.");
}
catch (SqlException)
{
MessageBox.Show("Error Occurs!");
}
}
}
System.Data.SqlClient.SqlException (0x80131904): The variable name '@ssn' has already been declared. Variable names must be unique within a query batch or stored procedure.
System.Data.SqlClient.SqlException(0x80131904):已声明变量名'@ssn'。变量名在查询批处理或存储过程中必须是唯一的。
3 个解决方案
#1
1
The error seems pretty clear - you're adding two parameters with the same name ("@ssn"
). You have some options:
错误似乎很清楚 - 你要添加两个具有相同名称的参数(“@ssn”)。你有一些选择:
- Rename one of the paraters (in the SQL and the
Parameters.AddWithValue
call - Don't update the
SSN
colume since that's the key value you're using - Add an independent primary key column (like an auto-increment integer) and use that for lookups instead of SSN.
重命名其中一个paraters(在SQL和Parameters.AddWithValue调用中)
不要更新SSN colume,因为这是您正在使用的键值
添加一个独立的主键列(如自动增量整数)并将其用于查找而不是SSN。
#2
0
You have cmd.Parameters.AddWithValue("@ssn"...
2 times. The exception is normal.
你有cmd.Parameters.AddWithValue(“@ ssn”...... 2次。例外是正常的。
#3
0
The problem is that you're adding the parameter twice. If you're looking to change the value of the parameter after you've added it try changing the existing parameters value property with this cmd.Parameters("@ssn").value = "somevalue"
问题是你要添加两次参数。如果您想在添加参数后更改参数值,请尝试使用此cmd.Parameters(“@ ssn”)更改现有参数值属性.value =“somevalue”
#1
1
The error seems pretty clear - you're adding two parameters with the same name ("@ssn"
). You have some options:
错误似乎很清楚 - 你要添加两个具有相同名称的参数(“@ssn”)。你有一些选择:
- Rename one of the paraters (in the SQL and the
Parameters.AddWithValue
call - Don't update the
SSN
colume since that's the key value you're using - Add an independent primary key column (like an auto-increment integer) and use that for lookups instead of SSN.
重命名其中一个paraters(在SQL和Parameters.AddWithValue调用中)
不要更新SSN colume,因为这是您正在使用的键值
添加一个独立的主键列(如自动增量整数)并将其用于查找而不是SSN。
#2
0
You have cmd.Parameters.AddWithValue("@ssn"...
2 times. The exception is normal.
你有cmd.Parameters.AddWithValue(“@ ssn”...... 2次。例外是正常的。
#3
0
The problem is that you're adding the parameter twice. If you're looking to change the value of the parameter after you've added it try changing the existing parameters value property with this cmd.Parameters("@ssn").value = "somevalue"
问题是你要添加两次参数。如果您想在添加参数后更改参数值,请尝试使用此cmd.Parameters(“@ ssn”)更改现有参数值属性.value =“somevalue”