C#增删改查操作Access数据库之二(数据库的增加)

功能:添加四个TextBox控件,在这四个控件中分别输入要添加的ID,学号,姓名,年龄,性别信息,单击Insert按钮将数据添加到Access数据库中并通过datagridview显示添加后的数据库。此外,设置了学号为主键,在添加数据到数据库前要判断一下是否已存在这样的学号。

这里注意一下,我将OleDbConnection conn 设置为全局变量,并且只在show按钮控件中调用了GetConnection()方法,所以想要连接数据库只能首先单机show按钮。

也可以在每一个按钮控件中调用方法GetConnection()。

1.数据添加方法的实现

        public void DataInsert(TextBox TextBox1, TextBox TextBox2, TextBox TextBox3, TextBox TextBox4, TextBox TextBox5, OleDbConnection conn)
        { 
            if (CheckSameName(TextBox2))  //estimate whether there is same record
            {
                MessageBox.Show("不允许填写相同记录");
            }
            else
            {
                try
                {
                    string sql = "insert into Student values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
                    OleDbCommand com = new OleDbCommand(sql, conn);
                    // execute
                    com.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }

            }

        }
这里将TextBox中输入的数据作为参数传递给sql语句有两种可行的方法,这两种我都尝试过了。如果有人知道哪种更优些,可以给我留言探讨。

第一种:

                    string sqlstr = "insert into Student (ID,[学号],[姓名],[年龄],[性别]) values(@ID,@StuNo,@StuName,@Age,@Gender)";
                    OleDbCommand com = new OleDbCommand(sqlstr, conn);
                    //add parameters and assign values
                    com.Parameters.AddWithValue("@ID", TextBox1.Text);
                    com.Parameters.AddWithValue("@StuNo", TextBox2.Text);
                    com.Parameters.AddWithValue("@StuName", TextBox3.Text);
                    com.Parameters.AddWithValue("@Age", TextBox4.Text);
                    com.Parameters.AddWithValue("@Gender", TextBox5.Text);
第二种:

                    string sql = "insert into Student values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
                    OleDbCommand com = new OleDbCommand(sql, conn);

2.判断当前是否存在学号

        public bool CheckSameName(TextBox TextBox2) 
        {
            bool result = false;
            string sql = "select [学号] from Student where [学号]=@StuNo";
            OleDbConnection conn = GetConnection();
            conn.Open();
            OleDbCommand comd = new OleDbCommand(sql, conn);
            comd.Parameters.AddWithValue("@StuNo", TextBox2.Text);
            OleDbDataReader read = comd.ExecuteReader();
            if (read.HasRows)
            {
                result = true;
            }
            else
            {
                result = false;
            }
            conn.Close();
            read.Close();
            return result;
            }

3.调用方法,显示

        private void btn_Insert_Click(object sender, EventArgs e)
        {
            conn.Open();
            try
            {
                opera.DataInsert(textBox1, textBox2, textBox3, textBox4, textBox5, conn);
                DataDisplay();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                conn.Close();
            }
        }

最终结果如下。


  • 5
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值