C#将数据插入Access数据库

时间:2018-06-27 06:28:34

标签: c#

我是编程新手,但我无法将表单中的数据插入数据库。如果单击该按钮,它将落入else语句中,其中显示“连接失败”。

这是我的代码:

private void buttonAddrecord_Click(object sender, EventArgs e)
{
    OleDbConnection conn = new OleDbConnection();
    conn.ConnectionString = (@"Provider= Microsoft.ACE.OLEDB.12.0;Data Source=
     C:\Users\pc\Documents\Visual Studio 2015\Projects\GamefarmDB\GamefarmDB\Gamefarm.accdb;
        User ID = admin;");

    String WingbandNumber = wingbandnumberTextbox.Text;
    String DateOfHatch = dateofhatchTextbox.Text;
    String Markings = markingsTextbox.Text;
    String Bloodline = bloodlineTextbox.Text;
    String Broodhen = broodhenTextbox.Text;
    String Broodcock = broodcockTextbox.Text;

    OleDbCommand cmd = new OleDbCommand("INSERT into List (WingbandNumber, 
        DateOfHatch, Markings, Bloodline, Broodhen, Broodcock)  
        Values(@WingbandNumber, @DateOfHatch, @Markings, @Bloodline,  
        @Broodhen, @Broodcock)");

    cmd.Connection = conn;

    if (conn.State == ConnectionState.Open)
    {
        cmd.Parameters.Add("@WingbandNumber", OleDbType.Numeric).Value = WingbandNumber;
        cmd.Parameters.Add("@DateOfHatch", OleDbType.LongVarChar).Value = DateOfHatch;
        cmd.Parameters.Add("@Markings", OleDbType.LongVarChar).Value = Markings;
        cmd.Parameters.Add("@Bloodline", OleDbType.LongVarChar).Value = Bloodline;
        cmd.Parameters.Add("@Broodhen", OleDbType.LongVarChar).Value = Broodhen;
        cmd.Parameters.Add("@Broodcock", OleDbType.LongVarChar).Value = Broodcock;

        try
        {
            cmd.ExecuteNonQuery();
            MessageBox.Show("Data Added");
            conn.Close();
        }
        catch (OleDbException ex)
        {
            MessageBox.Show(ex.Source);
            conn.Close();
        }
    }
    else
    {
        MessageBox.Show("Connection Failed");
    }
 }

3 个答案:

答案 0 :(得分:0)

我同意您应该使用conn.Open();并且您还应该在if(conn.State == ConnectionState.Open)之前添加一个try catch,这样您的代码应该看起来像这样

.
.
.
cmd.Connection = conn;
try
    {
       conn.Open();
        if (conn.State == ConnectionState.Open)
        {
              .
              .
              .
    else
            {
                MessageBox.Show("Connection Failed");
            }
    catch (Exception ex)
{
MessageBox.Show(ex.toString());
conn.Close();
}

这样,它将为您提供无法连接的确切例外,并且如果整个过程出现任何问题,也会为您提供例外。

答案 1 :(得分:0)

命令对象需要打开连接。因此,您应该在执行命令

之前打开连接

答案 2 :(得分:0)

您只需要在实际使用连接对象之前打开它 这样您的代码将如下所示,

private void buttonAddrecord_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = (@"Provider= Microsoft.ACE.OLEDB.12.0;Data Source =C:\Users\pc\Documents\Visual Studio 2015\Projects\GamefarmDB\GamefarmDB\Gamefarm.accdb;User ID = admin;");
    String WingbandNumber = wingbandnumberTextbox.Text;
    String DateOfHatch = dateofhatchTextbox.Text;
    String Markings = markingsTextbox.Text;
    String Bloodline = bloodlineTextbox.Text;
    String Broodhen = broodhenTextbox.Text;
    String Broodcock = broodcockTextbox.Text;

    OleDbCommand cmd = new OleDbCommand("INSERT into List (WingbandNumber, DateOfHatch, Markings, Bloodline, Broodhen, Broodcock) Values(@WingbandNumber, @DateOfHatch, @Markings, @Bloodline, @Broodhen, @Broodcock)");

    cmd.Connection = conn;

    conn.Open(); // To Open Connection
    if (conn.State == ConnectionState.Open)
    {
        cmd.Parameters.Add("@WingbandNumber", OleDbType.Numeric).Value = WingbandNumber;
        cmd.Parameters.Add("@DateOfHatch", OleDbType.LongVarChar).Value = DateOfHatch;
        cmd.Parameters.Add("@Markings", OleDbType.LongVarChar).Value = Markings;
        cmd.Parameters.Add("@Bloodline", OleDbType.LongVarChar).Value = Bloodline;
        cmd.Parameters.Add("@Broodhen", OleDbType.LongVarChar).Value = Broodhen;
        cmd.Parameters.Add("@Broodcock", OleDbType.LongVarChar).Value = Broodcock;

        try
        {
            cmd.ExecuteNonQuery();
            MessageBox.Show("Data Added");
            conn.Close();
        }
        catch (OleDbException ex)
        {
            MessageBox.Show(ex.Source);
            conn.Close();
        }
    }
    else
    {
        MessageBox.Show("Connection Failed");
    }
}