简体   繁体   中英

C# Inserting data to Access Database

I am new to programming and I have a problem with inserting data from my form into the database. If I click the button, it falls into the else statement where it shows "Connection Failed".

Here is my code:

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");
    }
 }

I agree that you should have use conn.Open(); and also you should put a try catch before the if (conn.State == ConnectionState.Open) so your code should look like this

.
.
.
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();
}

So that it will give you the exact exception why you can't connect and also it will give you an exception if anything goes wrong to the whole precedure.

Command object requires open connection. So you should open connection before execute the command

You need to just open the connection object before actually using it so your code will look like this,

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");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM