search
HomeDatabaseMysql TutorialDataTable的事件响应

DataTable的事件响应

Jun 07, 2016 pm 03:07 PM
datatableeventSynchronizeresponsethisquestion

这个问题是由要同步两个数据库而引发的,我的想法是,对于两个同样内容的数据库(保存在Access和SqlServer中),使得在程序中修改一个表的时候对两个数据库的表同时更新。 当然,更新的工作交给Adapter,主要的技术是让从两数据库读取的Datatable修改同步起

这个问题是由要同步两个数据库而引发的,我的想法是,对于两个同样内容的数据库(保存在Access和SqlServer中),使得在程序中修改一个表的时候对两个数据库的表同时更新。
当然,更新的工作交给Adapter,主要的技术是让从两数据库读取的Datatable修改同步起来。
这里就要用到DataTable的事件响应,我用到的有:
增:DataTable.TableNewRow ; DataTable.RowChanged ;
删:DataTable.RowDeleting ; DataTable.RowDeleted ;
改:DataTable.ColumnChanged
下面以 表 T1 T2 为例说明这些事件如何被触发,以及触发时怎样处理。
增:
新建一个数据行,我们一般使用 T1.Rows.Add() 方法,这个方法将触发 T1.RowChanged 事件,说明一个新的行已经被加到 T1 中,索引也随之更新,但是这个过程无法让用户编辑这个行(一次写入);而为了让用户在添加新行之前进行编辑(以避免无法自定义验证的情况),需要在调用 T1.Rows.Add() 方法前声明一个DataRow 变量来保存新行的数据。
为简便计且让 DataTable 自行检查输入的合法性,在新行上使用 DataRow R1 = T1.NewRow();
这样实例化的 R1 将带有 T1 的 schema 结构。
在调用 T1.NewRow() 方法时,即触发 T1.TableNewRow 事件;值得注意的是,此时这个数据行并没有真正加入到 T1.Rows 集合中,而当对 R1 的各个成员赋值时,将依次触发 DataTable.ColumnChanged 事件,最后调用 T1.Rows.Add( R1 ); ,这时新行的增加就可以在 T1.RowChanged 事件中利用 e.Action 属性捕获到。
如此一来,即有如下处理:
修改命令? 触发的事件? 同步处理
DataRow R1 = T1.NewRow();? T1.TableNewRow? DataRow R2 = T2.NewRow();?
R1[0] = v1;? T1.ColumnChanged? R2[0] = v1;
T1.Rows.Add(R1);? T1.RowChanged? T2.Rows.Add(R2);
删:
由于我们假设两个数据库的内容一致,删除的时候,只需要知道 T1 中的哪一行被删除,即可做出正确的处理。
在删除时,T1.Rows.RemoveAt(0); 和 T1.Rows[0].Delete(); 是等效的,均将依次触发 DataTable.RowDeleting 和 DataTable.RowDeleted 事件
为了得到被删除行原本所在位置的索引,我们需要在其被删除前( DataTable.RowDeleted )捕获,即在 DataTable.RowDeleting 的响应中,通过 T1.Rows.IndexOf(e.Row) 返回得到;而在 DataTable.RowDeleted 的响应中我们将得到 -1 。
如此一来,即有如下处理:
修改命令? 触发的事件? 同步处理
T1.Rows[n].Delete();? DataTable.RowDeleting? int i = T1.Rows.IndexOf(e.Row);
-? DataTable.RowDeleted? T2.Rows[i].Delete();
改:
所谓的更改,即修改 DataTable 中某一元素( cell )的值。若要修改生效,应当使用如下格式的语句
T1.Rows[m][n] = v2 ;
笔者曾试用过 T1.Rows[m].ItemArray[n] = v2;语句,但赋值无效,这是因为 ItemArray 集合只接受从 Array 实例传递的批量值,使用如下语句:
object[] a1 = new object[1] { v3 };
T1.Rows[m].ItemArray = a1;
可以批量更改一行中的数据,这常用在新建行的过程中,不适合单个元素的赋值。
在赋值时,触发 DataTable.ColumnChanged 事件,通上例,可以获得被修改单元的行列索引和修改后的值。
如此一来,即有如下处理:
修改命令? 触发的事件? 同步处理
T1.Rows[m][n] = v2;? DataTable.ColumnChanged? int i = T1.Rows.IndexOf(e.Row);
int j = T1.Rows.IndexOf(e.Column);
T2.Rows[i][j] = e.ProposedValue;
最后,假如我们的 T1 和 T2 都是由 Adapter.Fill() 方法获得,那么这个过程当然将同时被两个 Adapter 记录,再调用 Adapter.Update() 方法就可实现同步更新。
另,这篇文章中没有考虑两数据库初始内容不同的情况。

reload note: http://blog.sina.com.cn/s/blog_4b2950c20100igfe.html
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software