返回Null值以通过OleDb访问数据库

时间:2016-07-22 16:17:46

标签: c# oledb

我已经设置了一个INSERT语句,如果文本框为空,则返回值到数据库,但插入时仍然失败。

我遇到'没有给出一个或多个必需参数的值。'

我哪里错了?

我的访问权限未设置为必需

private void NewCustomer_Load(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
    OleDbConnection Conn = new OleDbConnection();
    Conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BoilerSvc_be.mdb";
    OleDbCommand command = new OleDbCommand();
    command.CommandText = "INSERT INTO Contacts (Title,Initial,Surname,[Address 1],[Address 2],[Address 3],[Post Town],[Post Code],Telephone,Archived) VALUES (@Title,@FirstName,@LastName,@Address1,@Address2,@Address3,@PostTown,@PostCode,@Telephone,Archived = 0)";

    if (string.IsNullOrEmpty(FirstName.Text))
    {
        command.Parameters.AddWithValue("@FirstName", DBNull.Value);
    }
    else
    {
        command.Parameters.AddWithValue("@FirstName", title.Text);
    }

    if (string.IsNullOrEmpty(LastName.Text))
    {
        command.Parameters.AddWithValue("@LastName", DBNull.Value);
    }
    else
    {
        command.Parameters.AddWithValue("@LastName", title.Text);
    }

    if (string.IsNullOrEmpty(Address1.Text))
    {
        command.Parameters.AddWithValue("@Address1", DBNull.Value);
    }
    else
    {
        command.Parameters.AddWithValue("@Address1", title.Text);
    }

    if (string.IsNullOrEmpty(Address2.Text))
    {
        command.Parameters.AddWithValue("@Address2", DBNull.Value);
    }
    else
    {
        command.Parameters.AddWithValue("@Address2", title.Text);
    }

    if (string.IsNullOrEmpty(Address3.Text))
    {
        command.Parameters.AddWithValue("@Address3", DBNull.Value);
    }
    else
    {
        command.Parameters.AddWithValue("@Address3", title.Text);
    }

    if (string.IsNullOrEmpty(Postcode.Text))
    {
        command.Parameters.AddWithValue("@PostCode", DBNull.Value);
    }
    else
    {
        command.Parameters.AddWithValue("@PostCode", title.Text);
    }

    if (string.IsNullOrEmpty(TownCity.Text))
    {
        command.Parameters.AddWithValue("@PostTown", DBNull.Value);
    }
    else
    {
        command.Parameters.AddWithValue("@PostTown", title.Text);
    }

    if (string.IsNullOrEmpty(PhnNum.Text))
    {
        command.Parameters.AddWithValue("@Telephone", DBNull.Value);
    }
    else
    {
        command.Parameters.AddWithValue("@Telephone", title.Text);
    }

    if (string.IsNullOrEmpty(Titl.Text))
    {
        command.Parameters.AddWithValue("@Title", DBNull.Value);
    }
    else
    {
        command.Parameters.AddWithValue("@Title", title.Text);
    }

    Conn.Open();
    command.Connection = Conn;
    command.ExecuteNonQuery();
    Conn.Close();

    FirstName.Text = null;
    LastName.Text = null;
    Address1.Text = null;
    Address2.Text = null;
    Address2.Text = null;
    Postcode.Text = null;
    TownCity.Text = null;
    Titl.Text = null;
    PhnNum.Text = null;
    Address3.Text = null;

    MessageBox.Show("Customer Added");
}

2 个答案:

答案 0 :(得分:0)

根据评论,我不确定这是否适用于OleDb连接,但你应该重写你的代码,假设它接受命名参数。

private void button2_Click(object sender, EventArgs e)
{
    using(OleDbConnection Conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BoilerSvc_be.mdb"))
    {
        OleDbCommand command = new OleDbCommand();

        command.CommandText = "INSERT INTO Contacts (Title,Initial,Surname,[Address 1],[Address 2],[Address 3],[Post Town],[Post Code],Telephone,Archived) VALUES (@Title,@FirstName,@LastName,@Address1,@Address2,@Address3,@PostTown,@PostCode,@Telephone,0)";

        command.Parameters.AddWithValue("@FirstName", string.IsNullOrEmpty(FirstName.Text) ? DBNull.Value : title.Text);
        command.Parameters.AddWithValue("@LastName", string.IsNullOrEmpty(LastName.Text) ? DBNull.Value : title.Text);
        command.Parameters.AddWithValue("@Address1", string.IsNullOrEmpty(Address1.Text) ? DBNull.Value : title.Text);
        command.Parameters.AddWithValue("@Address2", string.IsNullOrEmpty(Address2.Text) ? DBNull.Value : title.Text);
        command.Parameters.AddWithValue("@Address3", string.IsNullOrEmpty(Address3.Text) ? DBNull.Value : title.Text);
        command.Parameters.AddWithValue("@PostCode", string.IsNullOrEmpty(Postcode.Text) ? DBNull.Value : title.Text);
        command.Parameters.AddWithValue("@PostTown", string.IsNullOrEmpty(TownCity.Text) ? DBNull.Value : title.Text);
        command.Parameters.AddWithValue("@Telephone", string.IsNullOrEmpty(PhnNum.Text) ? DBNull.Value : title.Text);
        command.Parameters.AddWithValue("@Title", string.IsNullOrEmpty(Titl.Text) ? DBNull.Value : title.Text);

        Conn.Open();
        command.Connection = Conn;
        command.ExecuteNonQuery();
    }

    FirstName.Text = null;
    LastName.Text = null;
    Address1.Text = null;
    Address2.Text = null;
    Address2.Text = null;
    Postcode.Text = null;
    TownCity.Text = null;
    Title.Text = null;
    PhnNum.Text = null;
    Address3.Text = null;

    MessageBox.Show("Customer Added");
}

答案 1 :(得分:0)

如上所述,对于OleDb参数,您必须改为使用问号:

private void button2_Click(object sender, EventArgs e)
{
    try
    {
        using (var Conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BoilerSvc_be.mdb"))
        {
            var command = new OleDbCommand("INSERT INTO Contacts (" + 
                                 "Title,   Initial,  Surname,[Address 1],[Address 2],[Address 3],[Post Town],[Post Code],Telephone,Archived)" +
                        " VALUES (    ?,         ?,        ?,          ?,          ?,          ?,          ?,          ?,        ?,       0)", Conn);
            Control[] controls = { Titl, FirstName, LastName,   Address1,   Address2,   Address3,   TownCity,   Postcode,   PhnNum };

            foreach (var control in controls)
                command.Parameters.AddWithValue("@" + control.Name,
                    string.IsNullOrEmpty(control.Text) ? DBNull.Value : control.Text as object);

            Conn.Open();
            if (command.ExecuteNonQuery() == 1)
            {
                MessageBox.Show("Customer Added");
                foreach (var control in controls)
                    control.Text = "";
            }
            else
                MessageBox.Show("Customer was not Added");

        } // Conn is closed and disposed at the end of the using block
    }
    catch (Exception ex) { 
        MessageBox.Show("Exception : " + ex.Message);
    }
}