参数@BranchName没有默认值

时间:2014-04-05 06:54:29

标签: c#

我正在尝试为我办公室的计算机部门开发库存控制系统。它将跟踪分支机器的来龙去脉,这些分支机构将被送到总公司进行维修。我还将使用crystal report来显示Microsoft Access 2010中的所有记录。当我单击输入表单的保存按钮时,会弹出此错误消息框。

(参数@BranchName没有默认值)

我使用Microsoft Access 2010,C#.NET和VS 2010.请帮帮我。

    private void btnSave_Click(object sender, EventArgs e)
    {
        {
            OleDbConnection connect = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\NLA\Documents\Visual Studio 2010\Projects\GlobalTreasureBank\GlobalTreasureBank\GTBDB.accdb;Jet OLEDB:Database Password=admin@GTB");
                try
                {
                    connect.Open();
                    OleDbCommand command = new OleDbCommand();
                    command.Connection=connect;
                    command.CommandText= "INSERT INTO Machine (BranchName, MachineName, ArrivedDate, FaultDetail, ReturnDate, Remark, Technician)" + "VALUES (@BranchName, @MachineName, @ArrivedDate, @FaultDetail, @ReturnDate, @Remark, @Technician)";

                    string BranchName = txtbBranch.Text;
                    string MachineName = txtbMachine.Text;
                    string FaultDetail = rtxtbFault.Text;
                    string Remark = rtxtbRemark.Text;
                    string Technician = txtbTechnician.Text;
                    DateTime ArrivedDate = dtpArrive.Value;
                    DateTime ReturnDate = dtpReturn.Value;

                    command.Parameters.Add("ArrivedDate", OleDbType.Date);
                    command.Parameters.Add("ReturnDate", OleDbType.Date);

                    command.Parameters.AddWithValue("@BranchName", txtbBranch.Text);
                    command.Parameters.AddWithValue("@MachineName", txtbMachine.Text);
                    command.Parameters.AddWithValue("@FaultDetail", rtxtbFault.Text);
                    command.Parameters.AddWithValue("@Remark", rtxtbRemark.Text);
                    command.Parameters.AddWithValue("@Technician", txtbTechnician.Text);
                    command.Parameters.AddWithValue("@ArrivedDate", dtpArrive.Value);
                    command.Parameters.AddWithValue("@ReturnDate", dtpReturn.Value);

                    command.ExecuteNonQuery();
                    MessageBox.Show ("Successfully write to database", "Write to Database", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    connect.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    connect.Close();
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

注1: OleDB不接受named parameters 注2: OleDB参数必须按顺序排列。

试试这个:

command.CommandText= "INSERT INTO Machine (BranchName, MachineName, ArrivedDate, 
   FaultDetail, ReturnDate, Remark, Technician) VALUES (?,?,?,?,?,?,?)";


command.Parameters.AddWithValue("@BranchName", txtbBranch.Text);
command.Parameters.AddWithValue("@MachineName", txtbMachine.Text);
command.Parameters.AddWithValue("@ArrivedDate", dtpArrive.Value);
command.Parameters.AddWithValue("@FaultDetail", rtxtbFault.Text);
command.Parameters.AddWithValue("@ReturnDate", dtpReturn.Value);
command.Parameters.AddWithValue("@Remark", rtxtbRemark.Text);
command.Parameters.AddWithValue("@Technician", txtbTechnician.Text);