惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
K
Kaspersky official blog
Scott Helme
Scott Helme
C
CXSECURITY Database RSS Feed - CXSecurity.com
GbyAI
GbyAI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Y
Y Combinator Blog
T
Threat Research - Cisco Blogs
L
LINUX DO - 热门话题
C
Cyber Attacks, Cyber Crime and Cyber Security
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
月光博客
月光博客
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
人人都是产品经理
人人都是产品经理
L
Lohrmann on Cybersecurity
Recorded Future
Recorded Future
Latest news
Latest news
V2EX - 技术
V2EX - 技术
T
The Exploit Database - CXSecurity.com
H
Heimdal Security Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
IT之家
IT之家
博客园 - 叶小钗
Microsoft Security Blog
Microsoft Security Blog
P
Proofpoint News Feed
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
PCI Perspectives
PCI Perspectives
AWS News Blog
AWS News Blog
H
Help Net Security
S
Security @ Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Announcements
Recent Announcements
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
F
Full Disclosure
S
Schneier on Security
S
Security Affairs
T
Tenable Blog

博客园 - 冯小磊

女人的面相 只有mdf文件的恢复技术 冒泡,选择,插入,希尔 vb.net c#.net 代码相互转换 [转]C# 参考之转换关键字:operator、explicit与implicit 【转载】SQL查询中区分大小写的方法 Dev里面DataGid控件使用方法之一 对于sql数据库中重复的记录... (word导出问题)解决:服务器出现意外情况。 (异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT))的解决方法 白骨精写给孙悟空的绝世情书 DBCC CHECKDB (检查指定数据库中的所有对象的分配和结构完整性) c# 限制窗体 sqlserver附加数据库错误823的解决方案 改变DevExpress控件的字体 按某一字段分组取最大(小)值所在行的数据 mssql1069错误(由于登录失败而无法启动服务)解决方法 C# 判断中文字符(字符串) 函数datediff MSSQL中取字符中的汉字或双字节字符
用SqlCommandBuilder 实现批量更新
冯小磊 · 2008-04-02 · via 博客园 - 冯小磊

()

 有没有人遇到这种情况,用 SqlDataAdapter.Update(ds)更新时出错?

 answer:   一般是这样的,如果用设计器将SqlDataAdapter拖到页面中使用时,不会出现这种情况,因为

                  系统会自动生成SqlDataAdapter的属性命令,比如: .UpdateCommane insertCommand

                  selectCommand等。 但是有些程序员不喜欢用设计器,或者是有些地方没必要拖动

                  SqlDataAdapter这么个庞大物来实现,那么SqlDataAdapter就不会自动生成相关的查询或更新

                  语句了.   所以当执行到SqlDataAdapter.Update(ds)语句时,SqlDataAdapter桥接器不知道更

                 新哪个表.不报错了

()

 解决方法:

 SqlCommandBuilder 实现批量更新

 1.功能:

   可以实现你对DataSetUI层做任意操作后,直接丢给这个方法,这个方法就可以自动把你的修改更 新到数 据库中,而没必要每次都更新到 数据库

 2.使用方法

        public void UpdateByDataSet(DataSet ds,string strTblName,string strConnection)
        
{
            
try
            
{
                SqlConnection  conn 
= new SqlConnection(strConnection);
         
                SqlDataAdapter myAdapter 
= new SqlDataAdapter();
                SqlCommand myCommand 
= new SqlCommand("select * from '"+strTblName+"' where 1=2 ",conn);    
                myAdapter.SelectCommand 
= myCommand;
                SqlCommandBuilder myCommandBuilder 
= new SqlCommandBuilder(myAdapter);     
                myAdapter.Update(ds,strTblName);  
            }
 
            
catch(Exception err)
            
{  
                
throw  err;
            }

        }

直接调用这个方法就可以啦,说明的一点是select * from '"+strTblName+"' where 1=2是一定要的,作用大家也应该想到了,主要是告诉 SqlDataAdapter更新哪个表

3.什么时候用?

    a. 有时候需要缓存的时候,比如说在一个商品选择界面,选择好商品,并且进行编辑/删除/更新后,

       最后一并交给数据库,而不是每一步操作都访问数据库,因为客户选择商品可能进行n次编辑/删除

       更新操作,如果每次都提交,不但容易引起数据库冲突,引发错误,而且当数据量很大时在用户执行

       效率上也变得有些慢

    b.有的界面是这样的有的界面是这样的,需求要求一定用缓存实现,确认之前的操作不提交到库,点击

      页面专门提交的按钮时才提交商品选择信息和商品的其它信息. 我经常遇到这样的情况

    c.有些情况下只往数据库里更新,不读取. 也就是说没有从数据库里读,SqlDataAdapter也就不知道是

      更新哪张表了,调用Update就很可能出错了。这样的情况下可以用SqlCommandBuilder .

4.

 注意点:

 1.只能更新一个表,不能更新两个或两个以上相关联的表

 2.表中必须有主键

 3.更新的表中字段不能有image类型的

5.优点:

    节省代码量,节省时间,这个方法可以代替所有的更新/删除/插入操作语句
作者Bloghttp://blog.csdn.net/ChengKing/