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

来源:互联网 发布:校园网络安全教育讲座 编辑:程序博客网 时间:2024/06/07 02:24

功能:添加四个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();            }        }

最终结果如下。


原创粉丝点击