C#窗体应用利用文本框对数据库一行多个数据进行增、删、该、查相关语句代码

先简单介绍一下我创建的窗体一些控件:
在这里插入图片描述
还在功能实现阶段,界面还没有美化,所以界面比较丑,原谅一下。
为了编码、便于记忆的方便,逐个将文本框的名字进行了修改。
在这里插入图片描述
(1)对数据库中表的内容进行查找:
代码如下:

 private void button1_Click(object sender, EventArgs e)
        {
            
            string connStr = @"Server=.; Initial Catalog=E-Genealogy; Integrated Security=True";//integrated security=true:集成验证,使用Windows验证的方式去连接到数据库服务器。
            string id = txtB2.Text;//数据库中成员ID是主键,也就是txtB2中的内容是成员ID
            SqlConnection conn = new SqlConnection(connStr);//建立一个b数据库连接对象conn
            conn.Open();//一个方法,用于打开conn所连接的数据库
            string sql = @"SELECT * FROM Member WHERE Member_ID='" + txtB2.Text + "'AND Genealogy_ID='"+txtB1.Text+"'";//通过成员ID和族谱ID来进行条件比较
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            if (!dr.Read())
            {
                MessageBox.Show("该成员不存在!");//未查找到就弹出提示框
                return;
            }
            //查找成功就在相应文本框中输入信息
            txtB3.Text = dr["Member_ID"].ToString();//将表格中成员ID输入到txtB3文本框中用于显示查找结果
            txtB4.Text = dr["Member_name"].ToString();
            txtB5.Text = dr["Member_sex"].ToString();
            txtB6.Text = dr["Member_birth"].ToString();
            txtB7.Text = dr["Member_died"].ToString();
            txtB8.Text = dr["Member_origin"].ToString();
            txtB9.Text = dr["Member_address"].ToString();
            txtB10.Text = dr["Member_live"].ToString();
            txtB11.Text = dr["Member_father"].ToString();
            txtB12.Text = dr["Member_spouseID"].ToString();
            txtB13.Text = dr["Genealogy_ID"].ToString();
            conn.Close();

        }

**string sql = @“SELECT * FROM Member WHERE Member_ID=’” + txtB2.Text + “‘AND Genealogy_ID=’”+txtB1.Text+"’";**这一句中有两个判定条件中间用AND连接。特别注意“=”右边的标点符号:先是一对单引号,里面再加入一对双引号。
(2)对表中内容进行修改:

 private void button3_Click(object sender, EventArgs e)
        {
            string connStr = @"Server=.; Initial Catalog=E-Genealogy; Integrated Security=True";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            string sql = @"UPDATE Member SET [Member_name]='" + txtB4.Text + "',[Member_sex]='" + txtB5.Text + "',[Member_birth]='" + txtB6.Text + "',[Member_died]='" + txtB7.Text + "',[Member_origin]='" + txtB8.Text + "',[Member_address]='" + txtB9.Text + "',[Member_live]='" + txtB10.Text + "',[Member_father]='" + txtB11.Text + "',[Member_spouseID]='" + txtB12.Text + "'";
            sql += " WHERE [Member_ID]='" + txtB2.Text + "'";  //判定条件,根据成员ID来对成员信息进行修改                                              
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("成员信息修改成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("更新出错!" + ex.Message);
            }
            conn.Close();

        }

string sql = @“UPDATE Member SET [Member_name]=’” + txtB4.Text + “’,[Member_sex]=’” + txtB5.Text + “’,[Member_birth]=’” + txtB6.Text + “’,[Member_died]=’” + txtB7.Text + “’,[Member_origin]=’” + txtB8.Text + “’,[Member_address]=’” + txtB9.Text + “’,[Member_live]=’” + txtB10.Text + “’,[Member_father]=’” + txtB11.Text + “’,[Member_spouseID]=’” + txtB12.Text + “’”;这一句是对表中9列属性进行修改(其中一个主键、一个外键不用修改)
特别注意属性与属性之间的标点符号

[Member_sex]=’" + txtB5.Text + "’,
等号左边是表中属性名,等号右边先加一对单引号,再加一对双引号,双引号里面为相应文本框的内容,最后加逗号将其与后面的属性分隔开。
(3)对表中内容进行删除:
删除一个成员比较简单,代码如下:

 private void button4_Click(object sender, EventArgs e)
        {
            bool flag = false;//作为删除结果的标志信息
            string connStr = @"Server=.; Initial Catalog=E-Genealogy; Integrated Security=True";
            string id = txtB2.Text;//通过成员ID这个主键来进行条件比较
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            string sql = @"DELETE FROM Member WHERE Member_ID='" + id + "'";//通过成员ID主键进行删除
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show(id + "号成员删除成功!");
                flag = true;//删除成功,标志变为true
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除出错!" + ex.Message);
            }
            conn.Close();
            //如果删除成,将文本框中的内容变为“***”,覆盖掉原来信息
            if(flag)
            {
                txtB3.Text = "***";
                txtB4.Text = "***";
                txtB5.Text = "***";
                txtB6.Text = "***";
                txtB7.Text = "***";
                txtB8.Text = "***";
                txtB9.Text = "***";
                txtB10.Text = "***";
                txtB11.Text = "***";
                txtB12.Text = "***";
                txtB13.Text = "***";
            }

(4)向表中添加一行多列数据:

private void button2_Click(object sender, EventArgs e)
        {
            string connStr = @"Server=.; Initial Catalog=E-Genealogy; Integrated Security=True";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            string sql = "insert into Member(Member_ID,Member_name,Member_sex,Member_live,Member_spouseID,Member_father,Member_address,Member_origin,Member_birth,Member_died,Genealogy_ID) values('" + txtB3.Text + "', '" + txtB4.Text + "', '" + txtB5.Text + "', '" + txtB10.Text + "', '" + txtB12.Text + "', '" + txtB11.Text + "', '" + txtB9.Text + "', '" + txtB8.Text + "', '" + txtB6.Text + "', '" + txtB7.Text + "', '" + txtB13.Text + "')";//插入信息
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("信息已存入数据库!");
            }
            catch (Exception msg)
            {
                MessageBox.Show("出问题了!\n出错原因:" + msg.Message);
            }
            conn.Close();
        }

string sql = “insert into Member(Member_ID,Member_name,Member_sex,Member_live,Member_spouseID,Member_father,Member_address,Member_origin,Member_birth,Member_died,Genealogy_ID) values(’” + txtB3.Text + “’, '” + txtB4.Text + “’, '” + txtB5.Text + “’, '” + txtB10.Text + “’, '” + txtB12.Text + “’, '” + txtB11.Text + “’, '” + txtB9.Text + “’, '” + txtB8.Text + “’, '” + txtB6.Text + “’, '” + txtB7.Text + “’, '” + txtB13.Text + “’)”;
这一句同样要注意列与列之间的符号问题,容易出错。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值