访问Access数据库

时间:2016-10-06 22:49:09

标签: c# ms-access

当我尝试调试连接代码(con.open())时,连接为空。

public partial class Form1 : Form
{
    OleDbConnection con;
    OleDbDataAdapter da;
    OleDbCommand cmd;
    DataSet ds;


    public Form1()
    {
        InitializeComponent();
    }

    void filldata()
    {
        con = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=yemekhane.accdb");
        da = new OleDbDataAdapter("Select *from isci", con);
        ds = new DataSet();
        con.Open();
        da.Fill(ds, "isci");
        dGrid_ymk.DataSource = ds.Tables["isci"];
        con.Close();
    }
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        tb_No.Text = dGrid_ymk.CurrentRow.Cells[0].Value.ToString();
        tb_Ad.Text = dGrid_ymk.CurrentRow.Cells[1].Value.ToString();
        tb_Syd.Text = dGrid_ymk.CurrentRow.Cells[2].Value.ToString();

    }

    private void btn_add_Click(object sender, EventArgs e)
    {
        cmd = new OleDbCommand();
        con.Open();
        cmd.Connection = con;
        cmd.CommandText = "insert into isci (isci_ad,isci_soyad,scl_no) values ('" + tb_Ad.Text + "','" + tb_Syd.Text + "','" + tb_No.Text + "')";
        cmd.ExecuteNonQuery();
        con.Close();
        filldata();
    }

    private void btn_upd_Click(object sender, EventArgs e)
    {
        cmd = new OleDbCommand();
        con.Open();
        cmd.Connection = con;
        cmd.CommandText = "update isci set isci_ad='" + tb_Ad.Text + "',isci_syd='" + tb_Syd.Text + "' where scl_no=" + tb_No.Text + "";
        cmd.ExecuteNonQuery();
        con.Close();
        filldata();
    }

    private void btn_del_Click(object sender, EventArgs e)
    {
         cmd = new OleDbCommand();
        con.Open();
        cmd.Connection = con;
        cmd.CommandText = "delete from isci where scl_no="+tb_No.Text+"";
        cmd.ExecuteNonQuery();
        con.Close();
        filldata();
    }
}

0 个答案:

没有答案