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

发布于 2024-10-31 08:21:29 字数 3409 浏览 0 评论 0原文

我正在使用 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");` 中显示错误

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

请帮助我。

I am creating this project using Microsoft Visual C# 2008 Express Edition.
I want insert data using radio buttons how to write the code for example, i want insert gender (male/female). Please help me to write the code

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

    }
}

When run this it shows error in da.Update(ds1, "MyWorkers1");` like

Invalid OperationException Unhandled connection property has not been intialized

Please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫竹語嫣☆ 2024-11-07 08:21:29

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

You need to open your connection again in the Save method like you do in the FormLoad method.

空城仅有旧梦在 2024-11-07 08:21:29

您忘记在更新之前打开连接。

最佳实践是在事务之前和之后打开和关闭连接。

首先,不要通过从函数中删除 con.Dispose(); 来释放 Form1_Load() 中的连接。

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

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

这应该可以解决问题。

You forgot to open your connection before the update.

Best practice is to open and close a connection right before and after a transaction.

First off do not dispose the connection in your Form1_Load() by removing con.Dispose(); from the function.

Then add the following around your update call in your btnSave_Click eventhandler:

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

That should do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文