DataGridView里批量修改提交数据库方法

本文介绍两种使用C#进行SQL数据更新的方法:一是通过SqlCommandBuilder自动生成更新语句;二是手动指定UpdateCommand,包括参数设置及数据同步策略。
部署运行你感兴趣的模型镜像

准备工作,声明窗体级全局变量:

        private DataSet ds = new DataSet();
        private SqlDataAdapter sda = new SqlDataAdapter();
        private SqlConnection myConn = new SqlConnection();

1、利用SqlCommandBuilder

      SqlCommandBuilder sb = new SqlCommandBuilder(sda);
      sda.Update(ds.Tables[0]);
      ds.Tables[0].AcceptChanges(); 

 

2、指定UpdateCommand

 SqlParameter param = new SqlParameter();

            string strSql = " update  Good_PrtyInfo set   maxstcs=@maxstcs  ,  minstcs=@minstcs   where 1=1  and GoodID=@GoodID ";
            sda.UpdateCommand = new SqlCommand(strSql, myConn);

            param = sda.UpdateCommand.Parameters.Add("@maxstcs", SqlDbType.VarChar, 50, "maxstcs");
            param.SourceVersion = DataRowVersion.Current;

            param = sda.UpdateCommand.Parameters.Add("@minstcs", SqlDbType.VarChar, 50, "minstcs");
            param.SourceVersion = DataRowVersion.Current;

            //param = myDA.UpdateCommand.Parameters.Add("@DeptID", SqlDbType.VarChar, 50, "DeptID");
            //param.SourceVersion = DataRowVersion.Current;

            param = sda.UpdateCommand.Parameters.Add("@GoodID", SqlDbType.VarChar, 50, "GoodID");
            param.SourceVersion = DataRowVersion.Original;

            sda.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;

            sda.UpdateBatchSize = 10;
            //SqlCommandBuilder sb = new SqlCommandBuilder(sda);
            sda.Update(ds.Tables[0]);
            ds.Tables[0].AcceptChanges(); 

您可能感兴趣的与本文相关的镜像

GPT-SoVITS

GPT-SoVITS

AI应用

GPT-SoVITS 是一个开源的文本到语音(TTS)和语音转换模型,它结合了 GPT 的生成能力和 SoVITS 的语音转换技术。该项目以其强大的声音克隆能力而闻名,仅需少量语音样本(如5秒)即可实现高质量的即时语音合成,也可通过更长的音频(如1分钟)进行微调以获得更逼真的效果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值