当前位置: 首页 > 编程日记 > 正文

在DataTable中更新、删除数据

在DataTable中选择记录

 /*在DataTable中选择记录*//* 向DataTable中插入记录如上,更新和删除如下:* ----但是在更新和删除前,首先要找出要更新和删除的记录。* 一种方法是遍历DataRow,搜索想要的记录,* --〉然而更聪明的办法是使用DataTable.Select方法:table.Select();* table.Select();//返回DataRow[],可以包含一行或多行,* 取决于Select()的参数(filter)* gets an array of all Data.DataRow objects that match the* filter in the order of the sort * that match the specified state*/DataRow[] arrRows = table.Select("title_id='JP1001'");/** 下面这条语句选择(返回)"Price"字段值小于10的DataRow*/DataRow[] arrRows = table.Select("price<10.00");/** 下面这条语句选择Pubdate字段日期在2000年1月1日当天或之后的DataRow */DataRow[] arrRows = table.Select("pubdate>='#1/1/2000#'");/*如果想知道Select返回了多少行,读取数组的Length属性*//*传递给DataTable.Select()的筛选器表达式: * 下面比较操作符可以被支持:<,<=,=,>=,>和<>也可以使用IN和Like*///返回"state"等于CA、TN或WA的所有行DataRow[] arrRows = table.Select("state in('ca','tn','wa')");//返回"state"以CA开头的所有行DataRow[] arrRows = table.Select("state like 'ca*'");//还可以使用一些Sql函数,在DataTable中选择State字段为空的记录DataRow[] arrRows = table.Select("isnull(state,0)=0");//还可以使用And、Or和NotDataRow[] arrRows = table.Select("state='tn' and zip like '37*'");/*总之,可以用括号分组,创建复杂的布尔表达式*/

在DataTable中更新数据

/*在DataTable中更新数据*//** 确定了要在DataTable中更新的记录后,* 只要把记录的一个或多个字段替换成要更新的值即可,* 下面的例子选择Pubs数据库的Titles表中所有ytd_sales大于10000的记录* 把它们的价格加10.00*/SqlDataAdapter adapter = new SqlDataAdapter(strSql, strConn);DataSet ds = new DataSet();adapter.Fill(ds, "Titles");DataTable table = ds.Tables["Titles"];DataRow[] arrRows = table.Select("ytd_sales>10000");foreach (DataRow row in arrRows){row["price"] = (decimal)row["price"] + 10.00m;}
/*从DataTable中删除记录*//** 对每个要删除的行调用Delete就行了。*//** 下面删除ytd_sales小于10000或等于空的记录* */SqlDataAdapter adapter = new SqlDataAdapter(strSql, strConn);DataSet ds = new DataSet();adapter.Fill(ds, "Titles");DataTable table = ds.Tables["Titles"];DataRow[] arrRows = table.Select("ytd_sales<10000 or isnull(ytd_sales,0)=0");foreach (DataRow row in arrRows){row.Delete();}

把更改写回数据库DataAdapter.Update

/*把更改写回数据库DataAdapter.Update*//*Important: 这种在DataTable中进行的插入、更新和删除并不会自动写回数据库 */SqlDataAdapter adapter = new SqlDataAdapter(strSql, strConn);SqlCommandBuilder builder = new SqlCommandBuilder(adapter);DataSet ds = new DataSet();adapter.Fill(ds, "Titles");//插入记录DataTable table = ds.Tables["Titles"];DataRow row = table.NewRow();row["Title_id"] = "JP1001";row["title"] = "programming Microsoft .NET";row["price"] = 59.99m;row["ytd_sales"] = 100000;row["type"] = "business";row["pubdate"] = new DateTime(2002, 5, 1);table.Rows.Add(row);//更新数据库
            adapter.Update(table);/*DataAdapter的Update方法检查传递给表的每一条记录,把自从上次更新* (或自从上次调用table的AcceptChanges方法后)被插入、更新或删除的行* 写回数据库。如果DataSet中包含了多个被修改的DataTable,就把整个DataSet* 传给Update方法: adapter.Update(ds),所有改变会被一次性写回。*//** 哦,还有许多演示DataAdapter.Update的用法的示例,演示了通过调用名为GetChanges* 的方法创建一个只含有被插入、更新或删除行的新的临时DataSet或者DataTable* ,然后把delta传递给DataAdapter.Update,如下:* //更新数据库* DataTable delta = table.GetChanges();* adapter.Update(delta);* 这种方法的确好用,但不是必需的。Update会忽略包含已修改和未修改的行的DataTable中* 未被修改的行。* 当要控制写回数据库中的更改的内容的顺序时,GetChanges方法发挥作用,* 如果想在Insert前面执行Delete,以避免主键重复错误,应该这样做: *///DataRowState ---->enum System.Data.DataRowState//DataRowState.Deleted---->the row was deleted //                        using the row.Delete()DataTable deletes = table.GetChanges(DataRowState.Deleted);adapter.Update(deletes);DataTable inserts = table.GetChanges(DataRowState.Added);adapter.Update(inserts);/*GetChanges的另一个用处是,* 当更新不是在本地执行时,把机器间的数据传输量减到最小,* 只传递被改变的DataSet或DataTable比传递整个DataSet或DataTable更高效              */

转载于:https://www.cnblogs.com/jackcheblog/p/6738807.html

相关文章:

使用TensorFlow进行机器学习即服务

by Kirill Dubovikov通过基里尔杜博维科夫(Kirill Dubovikov) 使用TensorFlow进行机器学习即服务 (Machine Learning as a Service with TensorFlow) Imagine this: you’ve gotten aboard the AI Hype Train and decided to develop an app which will analyze the effective…

浏览器加载、解析、渲染的过程

最近在学习性能优化&#xff0c;学习了雅虎军规 &#xff0c;可是觉着有点云里雾里的&#xff0c;因为里面有些东西虽然自己也一直在使用&#xff0c;但是感觉不太明白所以然&#xff0c;比如减少DNS查询&#xff0c;css和js文件的顺序。所以就花了时间去了解浏览器的工作&…

《转》java设计模式--工厂方法模式(Factory Method)

本文转自&#xff1a;http://www.cnblogs.com/archimedes/p/java-factory-method-pattern.html 工厂方法模式&#xff08;别名&#xff1a;虚拟构造&#xff09; 定义一个用于创建对象的接口&#xff0c;让子类决定实例化哪一个类。Factory Method使一个类的实例化延迟到其子类…

微信小程序去除左上角返回的按钮

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 解决方法有两种&#xff1b; 1.把该页面设置为tab页面或者主页 ; 2.进入该页面使用 wx.reLaunch(); 示例 wx.reLaunch({url: ../detail/detail,}) 这样有一个弊端&#xff0c;就是…

我的第一个web_登陆我的第一个全栈Web开发人员职位

我的第一个webby Robert Cooper罗伯特库珀(Robert Cooper) 登陆我的第一个全栈Web开发人员职位 (Landing My First Full Stack Web Developer Job) This is the story of the steps I took to get my first job as a full stack web developer. I think it’s valuable to sha…

HTTP请求报文和HTTP响应报文(转)

原文地址&#xff1a;http://blog.csdn.net/zhangliang_571/article/details/23508953 HTTP报文是面向文本的&#xff0c;报文中的每一个字段都是一些ASCII码串&#xff0c;各个字段的长度是不确定的。HTTP有两类报文&#xff1a;请求报文和响应报文。 HTTP请求报文 一个HTTP请…

微信小程序用户未授权bug解决方法,微信小程序获取用户信息失败解决方法

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; bug示例图&#xff1a; 导致这个bug的原因是 wx.getUserInfo(OBJECT) 接口做了调整&#xff1b; 请看官方文档的描述&#xff1a; wx.getUserInfo(OBJECT) 注意&#xff1a;此接口有…

格式化json日期'/Date(-62135596800000)/'

日期经过json序列化之后&#xff0c;变成了/Date(-62135596800000)/字符串&#xff0c;在显示数据时&#xff0c;我们需要解释成正常的日期。 Insus.NET和js库中&#xff0c;写了一个jQuery扩展方法&#xff1a; $.extend({JsonDateParse: function (value) {if (value /Date(…

aws lambda使用_使用AWS Lambda安排Slack消息

aws lambda使用Migrating to serverless brings a lot of questions. How do you do some of the non-serverless tasks, such as a cronjob in a serverless application?迁移到无服务器带来了很多问题。 您如何执行一些非无服务器的任务&#xff0c;例如无服务器应用程序中的…

微信小程序模块化开发 include与模板开发 template

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 1. include 是引用整个wxml文件&#xff0c;我通常会配合js&#xff0c;css一起使用&#xff1b; 使用场景&#xff0c;需要封装事件和微信 api 的公共模块。 2.template &#xff…

winform解析json

在使用C#开发爬虫程序时&#xff0c;会遇到需要解析json字符串的情况。对于json字符串可以使用正则表达式的形式进行解析&#xff0c;更为方便的方法是使用Newtonsoft.Json来实现。 Nuget添加应用包 在工程上右键——【管理Nuget程序包】浏览找到要安装的程序包Newtonsoft.Jso…

Oracle11g密码忘记处理方法

c:\>sqlplus /nolog sql>connect / as sysdba sql>alter user 用户名 identified by 密码;&#xff08;注意在这里输入的密码是区分大小写的&#xff09; 改完之后你可以输入 sql>connect 用户名/密码 as sysdba进行验证 转载于:https://www.cnblogs.com/imhuanxi…

hic染色体构想_了解微服务:从构想到起点

hic染色体构想by Michael Douglass迈克尔道格拉斯(Michael Douglass) 了解微服务&#xff1a;从构想到起点 (Understanding Microservices: From Idea To Starting Line) Over the last two months, I have invested most of my free time learning the complete ins-and-outs…

[python]关于字符串查找和re正则表达式的效率对比

最近需要在python中做大日志文件中做正则匹配 开始直接在for in 中每行做re.findall&#xff0c;后来发现&#xff0c;性能不行&#xff0c;就在re前面做一个基本的字符串包含判断 (str in str)&#xff0c;如果不包含直接continue 效率对比&#xff1a; 1、只做一次包含判断&a…

微信小程序客服功能 把当前页面的信息卡片发送给客服

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 需求&#xff1a;微信小程序客服带详情页 &#xff0c; 场景&#xff1a;一个人通过微信小程序接入微信客服&#xff0c;聊天后带上入口链接 效果图&#xff1a; 写法&#xff1a; …

phpcms标签大全V9

转自&#xff1a;http://blog.csdn.net/cloudday/article/details/7343448调用头部 尾部{template "content","header"} 、 {template "content","footer"}{siteurl($siteid)} 首页链接地址 <a href"{siteurl($siteid)}/&q…

多伦多到温莎_我想要freeCodeCamp Toronto的Twitter来发布报价,所以我做了一个免费的bot来做到这一点。...

多伦多到温莎If you read About time, you’ll know that I’m a big believer in spending time now on building things that save time in the future. To this end, I built a simple Twitter bot in Go that would occasionally post links to my articles and keep my ac…

Linux常用命令汇总(持续更新中)

命令说明注意点cat access.log | wc -l统计行数awk命令可以做到同样的想过&#xff1a;cat access.log | awk END {print NR}grep vnc /var/log/messages查看系统报错日志等同于&#xff1a;sudo dmesg -T | grep "(java)"netstat -lnt | grep 590*查看端口状态 nets…

IOS问题汇总:2012-12-18 UIAlertView+UIActionSheet

UIAlertView/UIActionSheet UIAlertView * alertView [[UIAlertView alloc] initWithTitle:“添加场景模式” message:“请输入场景名称” delegate:self cancelButtonTitle:“取消” otherButtonTitles:“确定”, nil];alertView.alertViewStyle UIAlertViewStylePlainTextI…

PHP入门 1 phpstudy安装与配置站点

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 1&#xff0c; 一键安装 phpstudy &#xff1b; 点击跳转下载&#xff1b; 2.配置站点&#xff0c;点击MySQL 其它选项菜单的站点域名管理&#xff1b;再点击新增 2&#xff0c;点击其他选项菜单点击打开…

singleton设计模式_让我们研究一下Singleton设计模式的优缺点

singleton设计模式by Navdeep Singh通过Navdeep Singh 让我们研究一下Singleton设计模式的优缺点 (Let’s examine the pros and cons of the Singleton design pattern) Design patterns are conceptual tools for solving complex software problems. These patterns are si…

【转】MFC消息映射详解(整理转载)

消息&#xff1a;主要指由用户操作而向应用程序发出的信息&#xff0c;也包括操作系统内部产生的消息。例如&#xff0c;单击鼠标左按钮&#xff0c;windows将产WM_LBUTTONDOWN消息&#xff0c;而释放鼠标左按钮将产生WM_LBUTTONUP消息&#xff0c;按下键盘上的字母键&#xff…

php 2 往数据库添加数据

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 前端代码&#xff1a; function submit_result() { $.post("Controllers/ajaxController.php",{"name": $("#name").val(),"mobile": $("#mo…

设计模式:单例

传统的实现方法&#xff1a;两私一公&#xff0c;涉及线程安全问题&#xff08;即使有多重检查锁也可以通过反射破坏单例&#xff09;public class Singleton {private volatile static Singleton instance null;private Singleton () {}public static Singleton getSingleton…

100天59万行代码_如何抽出100天的代码时间

100天59万行代码Life moves pretty fast. If you don’t stop and look around once in a while, you could miss it. — Ferris Bueller生活发展很快。 如果您不停地走动&#xff0c;不时环顾四周&#xff0c;您可能会错过它。 —摩天轮 My time at freeCodeCamp was a fun an…

Mac 安装SecureCRT

scrt-8.0.2-1118.osx_x64.dmg https://pan.baidu.com/s/1miS5XVy 1.下载破解文件 SecureCRT https://pan.baidu.com/s/1eRW5IfS 2. 打开终端执行 chmod x ~/Downloads/SecureCRT 替换破解文件SecureCRT到/Applications/SecureCRT.app/Contents/MacOS/ 3. 打开SecureCRT&#xf…

PHP 3 HTML POST带参数请求 后端返回json格式的数据给前端

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 前端代码 <!DOCTYPE html> <html lang"zh"><head><meta charset"UTF-8"><title>Title</title><script src"https://ajax.aspnetcdn.c…

产品经理入门_所以您想成为产品经理? 这就是我的入门方式。

产品经理入门by Melanie Lei由Melanie Lei 所以您想成为产品经理&#xff1f; 这就是我的入门方式。 (So you want to be a product manager? This is how I got started.) One of the most common questions I get asked is, “How do you get a Product Manager job if you…

又拍云SSL证书全新上线,提供一站式HTTPS安全解决方案

互联网快速发展&#xff0c;云服务早已融入每一个人的日常生活&#xff0c;而互联网安全与互联网的发展息息相关&#xff0c;这其中涉及到信息的保密性、完整性、可用性、真实性和可控性。又拍云上线了与多家国际顶级 CA 机构合作的数款OV & EV SSL证书&#xff0c;提供一站…

HDU 1155 Bungee Jumping

题意&#xff1a;英语水平太次…………读了好久好久好久才读懂OTZ James Bond要逃跑&#xff0c;跑到一个桥边上&#xff0c;要跳到地面&#xff0c;桥边有个有弹性的绳子长度为l&#xff0c;如果他跳下去能到达地面&#xff0c;但速度超过10就会摔死&#xff0c;否则能成功降落…