C# SqlCommandBuilder封装DRUD命令,更新数据库

SqlCommandBuilder帮助我们的Adapter生成相关的CRUD 的SqlCommand。也可以手动为Adapter写增删查改的SqlCommand命令。

SqlCommandBuilder实现对数据库的更新:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _01SqlDataAdapterDemo
{
    public partial class SqlCommandBuilderCRUD : Form
    {
        public SqlCommandBuilderCRUD()
        {
            InitializeComponent();
        }

        private void SqlCommandBuilderCRUD_Load(object sender, EventArgs e)
        {
            string connSql = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            string sql =
               @"Select UserId, UserName, UserAge, DelFlag, CreateDate, UserPwd, LastErrorDateTime, ErrorTimes from userInfo";

            using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connSql))
            {
                DataTable dt =new DataTable();
                adapter.Fill(dt);
                this.dgvUserInfoCRUD.DataSource = dt;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //把DataGridView的修改的数据保存到数据库中去。
             string connSql = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            
            //修改的sql一定要跟  查询的sql脚本一致。
            string sql =
               @"Select UserId, UserName, UserAge, DelFlag, CreateDate, UserPwd, LastErrorDateTime, ErrorTimes from userInfo";
            using (SqlConnection conn = new SqlConnection(connSql))
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter(sql, conn))
                {
                    //拿到修改完了之后的DataTable对象
                    DataTable dt = this.dgvUserInfoCRUD.DataSource as DataTable; 

                    //把修改完的内存表dt 变化映射到数据库中的表的变化。  DataGridView中的DataTable中数据的变化(增删查改),直接映射保存到数据库中。
                    //SqlCommandBuilder帮助我们的Adapter生成相关的CRUD 的SqlCommand
                    using (SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adapter))
                    {
                        adapter.Update(dt);
                    } 
                }
            }//end  using Conn
            MessageBox.Show("保存成功");  //DataGridView中的DataTable中数据的变化(增删查改),直接映射保存到数据库中。
        }
    }
}


为SqlDataAdapter手写 增删查改 的SqlCommand 命令,来实现对数据库的更新:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _01SqlDataAdapterDemo
{
    public partial class SqlCommandBuilderCRUD : Form
    {
        public SqlCommandBuilderCRUD()
        {
            InitializeComponent();
        }

        private void SqlCommandBuilderCRUD_Load(object sender, EventArgs e)
        {
            string connSql = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            string sql =
               @"Select UserId, UserName, UserAge, DelFlag, CreateDate, UserPwd, LastErrorDateTime, ErrorTimes from userInfo";

            using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connSql))
            {
                DataTable dt =new DataTable();
                adapter.Fill(dt);
                this.dgvUserInfoCRUD.DataSource = dt;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //把DataGridView的修改的数据保存到数据库中去。
             string connSql = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            
            //修改的sql一定要跟  查询的sql脚本一致。
            string sql =
               @"Select UserId, UserName, UserAge, DelFlag, CreateDate, UserPwd, LastErrorDateTime, ErrorTimes from userInfo";
            using (SqlConnection conn = new SqlConnection(connSql))
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter(sql, conn))
                {
                    //adapter.in
                    //拿到修改完了之后的DataTable对象
                    DataTable dt = this.dgvUserInfoCRUD.DataSource as DataTable; //

                    #region 手写 删除 SqlCommand
                    //删除的Command 
                    adapter.DeleteCommand = conn.CreateCommand();
                    adapter.DeleteCommand.CommandText = "delete from UserInfo where UserId=@UserId";

                    //执行删除操作,把 UserId列的值 给@UserId参数用。
                    adapter.DeleteCommand.Parameters.Add("@UserId",SqlDbType.Int,4,"UserId");

                    #endregion

                    #region 手写 修改的 SqlCommand
                    adapter.UpdateCommand = conn.CreateCommand();
                    adapter.UpdateCommand.CommandText = "update  UserInfo set UserName=@UserName,UserAge=@UserAge  where UserId=@UserId";

                    //执行删除操作,把 UserId列的值 给@UserId参数用。
                    adapter.UpdateCommand.Parameters.Add("@UserId", SqlDbType.Int, 4, "UserId");
                    adapter.UpdateCommand.Parameters.Add("@UserName", SqlDbType.NVarChar, 32, "UserName");
                    adapter.UpdateCommand.Parameters.Add("@UserAge", SqlDbType.Int, 4, "UserAge");
                    #endregion

                    adapter.Update(dt);
                }
            }//end  using Conn
            MessageBox.Show("保存成功");
        }
    }
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值