c# winform对数据库进行增删改查操作

开发工具:sqlserver2012,visoual code 2017

打开sqlserver2012 ,创建一个表,表结构如下

c# winform对数据库进行增删改查操作

 然后打开VS2017,文件-新建-项目-Windows窗体应用 这里我就在工具箱拉取了三个button和一个显示数据的dataGirdView控件

c# winform对数据库进行增删改查操作

还有一个点击新增的跳转页面,有点丑不要介意哈

 

c# winform对数据库进行增删改查操作

 这里开始正式代码了:

先右击添加一个类,DataBase.cs 获取数据库的连接 

 //数据库连接
        public SqlConnection getConn()
        {
            String strConnection = "server=192.168.1.192;database=test1;uid=sa;pwd=1120061929";
            SqlConnection conn = new SqlConnection(strConnection);
            return conn;
        }

双击Form1.cs里的 四个按钮(增删查改) 生成四个点击事件

c# winform对数据库进行增删改查操作

这里我就直接把增删查改的代码写上去了

    //查询
        private void button1_Click(object sender, EventArgs e)
        {
            //获取数据库连接
            DataBase db = new DataBase();
            SqlConnection conn=db.getConn();
            //打开连接
            conn.Open();
           //定义sql语句
            String sql = "select * from test1";
            //创建sql执行对象
            SqlCommand comm = new SqlCommand(sql, conn);
            //创建数据适配器
            SqlDataAdapter sda = new SqlDataAdapter();
            //执行sql语句
            sda.SelectCommand = comm;
            //创建数据集
            DataSet ds = new DataSet();
            //将对应test表数据保存到数据集合中
            sda.Fill(ds, "test1");
            //把数据集合中第一个表的数据填充到dataGirdView中
            dataGridView1.DataSource = ds.Tables[0];
            //关闭连接
            conn.Close();
         
        }
        //新增
        private void button2_Click(object sender, EventArgs e)
        {
            add ad = new add();
            ad.ShowDialog();
        }
        //修改
        private void button3_Click(object sender, EventArgs e)
        {
            DataBase db = new DataBase();
            SqlConnection conn = db.getConn();
            conn.Open();
            int rowindex = dataGridView1.CurrentRow.Index;
            String value0 = dataGridView1.Rows[rowindex].Cells[0].Value.ToString();
            String value1 = dataGridView1.Rows[rowindex].Cells[1].Value.ToString();
            String value2 = dataGridView1.Rows[rowindex].Cells[2].Value.ToString();
            String sql = "update test1 set name='" + value1 + "',age='" + value2 + "' where id='" + value0 + "'";
            SqlCommand comm = new SqlCommand(sql, conn);
            comm.ExecuteNonQuery();
            MessageBox.Show("保存成功!");
            conn.Close();
        }
        //删除
        private void button4_Click(object sender, EventArgs e)
        {
            DataBase db = new DataBase();
            SqlConnection conn = db.getConn();
            conn.Open();
            int rowindex = dataGridView1.CurrentRow.Index;
            String value0 = dataGridView1.Rows[rowindex].Cells[0].Value.ToString();
            String sql="delete from test1 where id='"+value0+"'";
            SqlCommand comm = new SqlCommand(sql, conn);
            comm.ExecuteNonQuery();
            MessageBox.Show("删除成功!");
            conn.Close();
        }

效果图如下

c# winform对数据库进行增删改查操作

 

这里点新增的时候 跳转页面是直接new 新窗体类名调用他的showdialog()方法就行,这里就不介绍了 直接附上代码和效果图

  //新增
        private void button2_Click(object sender, EventArgs e)
        {
            add ad = new add();
            ad.ShowDialog();
        }
 private void button1_Click(object sender, EventArgs e)
        {
            DataBase db = new DataBase();
            SqlConnection conn = db.getConn();
            conn.Open();
            String sql="insert into test1 values('"+textBox1.Text+"','"+textBox2.Text+"')";
            SqlCommand comm = new SqlCommand(sql, conn);
            comm.ExecuteNonQuery();
            MessageBox.Show("保存成功!");
            conn.Close();
        }

效果图

c# winform对数据库进行增删改查操作

 

这是小弟的第一篇博客,也是转学c#的一个入门小demo,大佬们多多指教!谢谢!勿喷。