如何使用C#在Access中创建,更新,删除数据?

时间:2011-04-08 13:40:14

标签: c# .net winforms ms-access ado.net

我正在使用Microsoft Visual C#2008 Express Edition创建此项目。 我想使用单选按钮插入数据如何编写代码,例如,我想插入性别(男/女)。请帮我写代码

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        OleDbConnection con;
        DataSet ds1;
        OleDbDataAdapter da;

        int MaxRows = 0;
        int inc = 0;

        private void Form1_Load(object sender, EventArgs e)
        {
            con = new.OleDbConnection();
            ds1 = new DataSet();

            con.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/MyWorkers1.mdb";

            string sql = "SELECT * from tblWorkers";
            da = new OleDbDataAdapter(sql, con);

            con.Open();
            da.Fill(ds1, "MyWorkers1");
            NavigateRecords();
            MaxRows = ds1.Tables["MyWorkers1"].Rows.Count;
            //MaxRows = ds1.Tables["MyWorkers1"].Rows[inc];
            //MessageBox.Show("Database open");

            con.Close();
            //MessageBox.Show("Database close");

            con.Dispose();
        }

        private void NavigateRecords()
        {
            DataRow drow = ds1.Tables["MyWorkers1"].Rows[inc];
            textBox1.Text = drow.ItemArray.GetValue(0).ToString();
            textBox2.Text = drow.ItemArray.GetValue(1).ToString();
            textBox3.Text = drow.ItemArray.GetValue(2).ToString();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (inc != MaxRows - 1)
            {
                inc++;
                NavigateRecords();
            }
            else
            {
                MessageBox.Show("No More Records");
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (inc > 0)
            {
                inc--;
                NavigateRecords();
            }
            else
            {
                MessageBox.Show("First Record");
            }
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {

            if (inc != 0)
            {
                inc = 0;
                NavigateRecords();
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (inc != MaxRows - 1)
            {
                inc = MaxRows - 1;
                NavigateRecords();
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbCommandBuilder cb;
            cb = new System.Data.OleDb.OleDbCommandBuilder(da);

            DataRow drow = ds1.Tables["MyWorkers1"].NewRow();

            drow[0] = textBox1.Text;
            drow[1] = textBox2.Text;
            drow[2] = textBox3.Text;

            ds1.Tables["MyWorkers1"].Rows.Add(drow);

            MaxRows = MaxRows + 1;
            inc = MaxRows - 1;

            da.Update(ds1, "MyWorkers1");

            MessageBox.Show("Record / Entry Added");
        }       

    }
}

运行时,它会在da.Update中显示错误(ds1,“MyWorkers1”);`like

  

无效的OperationException尚未初始化未处理的连接属性

请帮帮我。

2 个答案:

答案 0 :(得分:2)

您需要在Save方法中再次打开连接,就像在FormLoad方法中一样。

答案 1 :(得分:1)

您在更新前忘了打开连接。

最佳做法是在交易之前和之后打开和关闭连接。

首先,请不要通过从功能中删除Form1_Load()来处理con.Dispose();中的连接。

然后在btnSave_Click事件处理程序中添加以下更新调用:

con.Open();
da.Update(ds1, "MyWorkers1");
con.Close();

这应该可以解决问题。

相关问题