变量名称' @ArrivedDate'已经宣布。变量名在查询批处理或存储过程中必须是唯一的

时间:2014-04-07 05:59:47

标签: c# sql

private void btnSave_Click(object sender, EventArgs e)
    {
        SqlConnection connection = new SqlConnection("Data Source=NLA-HP;Initial Catalog=GTBDB;Persist Security Info=False;User ID=sa;Password=dbadmin");

        SqlCommand command = new SqlCommand("insert into Machine VALUES (@BranchName, @MachineName, @ArrivedDate, @FaultDetail, @ReturnDate, @Remark, @Technician)", connection);
        connection.Open();

        try
        {
            //Variables declaration
            string BranchName = cbBranches.Text;
            string MachineName = txtbMachine.Text;
            DateTime ArrivedDate = dtpArrive.Value;
            string FaultDetail = rtxtbFault.Text;
            DateTime ReturnDate = dtpReturn.Value;
            string Remark = rtxtbRemark.Text;
            string Technician = txtbTechnician.Text;

            //Add values
            command.Parameters.AddWithValue("@BranchName", BranchName);
            command.Parameters.AddWithValue("@MachineName", MachineName);
            command.Parameters.AddWithValue("@ArrivedDate", ArrivedDate);
            command.Parameters.AddWithValue("@FaultDetail", FaultDetail);
            command.Parameters.AddWithValue("@ReturnDate", ReturnDate);
            command.Parameters.AddWithValue("@Remark", Remark);
            command.Parameters.AddWithValue("@Technician", Technician);

            command.Parameters.Add("ArrivedDate", SqlDbType.DateTime);
            command.Parameters.Add("ReturnDate", SqlDbType.DateTime);

            command.ExecuteNonQuery();

            MessageBox.Show("Successfully write to database", "Write to Database", MessageBoxButtons.OK, MessageBoxIcon.Information);
            connection.Close();
        }
        catch (Exception ex) //Catch exception
        {
            MessageBox.Show(ex.Message);
            connection.Close();
        }
    }

2 个答案:

答案 0 :(得分:1)

好吧,你添加两个参数两次:

第一次

command.Parameters.AddWithValue("@ArrivedDate", ArrivedDate);
command.Parameters.AddWithValue("@ReturnDate", ReturnDate);

第二次

command.Parameters.Add("ArrivedDate", SqlDbType.DateTime);
command.Parameters.Add("ReturnDate", SqlDbType.DateTime);

要为参数指定SQL类型并一次性指定值,请使用:

command.Parameters.Add("ArrivedDate", SqlDbType.DateTime).Value = ArrivedDate;
command.Parameters.Add("ReturnDate", SqlDbType.DateTime).Value = ReturnDate;

答案 1 :(得分:0)

我会让你轻松:

//Add values
command.Parameters.AddWithValue("@BranchName", BranchName);
command.Parameters.AddWithValue("@MachineName", MachineName);
/************************************************
  THIS LINE BELOW HERE HAS THE PARAMETER NAME
 ************************************************/
command.Parameters.AddWithValue("@ArrivedDate", ArrivedDate);
command.Parameters.AddWithValue("@FaultDetail", FaultDetail);
command.Parameters.AddWithValue("@ReturnDate", ReturnDate);
command.Parameters.AddWithValue("@Remark", Remark);
command.Parameters.AddWithValue("@Technician", Technician);

/************************************************
  THIS LINE BELOW HERE HAS THE PARAMETER NAME TOO
 ************************************************/
command.Parameters.Add("ArrivedDate", SqlDbType.DateTime);
command.Parameters.Add("ReturnDate", SqlDbType.DateTime);