Home Database Mysql Tutorial DataTable的事件响应

DataTable的事件响应

Jun 07, 2016 pm 03:07 PM
datatable event Synchronize response this question

这个问题是由要同步两个数据库而引发的,我的想法是,对于两个同样内容的数据库(保存在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 of this Website
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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solve the problem of playing headphones and speakers at the same time in win11 Solve the problem of playing headphones and speakers at the same time in win11 Jan 06, 2024 am 08:50 AM

Generally speaking, we only need to use one of the headphones or speakers at the same time. However, some friends have reported that in the win11 system, they encountered the problem of headphones and speakers sounding at the same time. In fact, we can turn it off in the realtek panel and it will be fine. , let’s take a look below. What should I do if my headphones and speakers sound together in win11? 1. First find and open the "Control Panel" on the desktop. 2. Enter the control panel, find and open "Hardware and Sound" 3. Then find the "Realtek High Definition" with a speaker icon. Audio Manager" 4. Select "Speakers" and click "Rear Panel" to enter the speaker settings. 5. After opening, we can see the device type. If you want to turn off the headphones, uncheck "Headphones".

One or more items in the folder you synced do not match Outlook error One or more items in the folder you synced do not match Outlook error Mar 18, 2024 am 09:46 AM

When you find that one or more items in your sync folder do not match the error message in Outlook, it may be because you updated or canceled meeting items. In this case, you will see an error message saying that your local version of the data conflicts with the remote copy. This situation usually happens in Outlook desktop application. One or more items in the folder you synced do not match. To resolve the conflict, open the projects and try the operation again. Fix One or more items in synced folders do not match Outlook error In Outlook desktop version, you may encounter issues when local calendar items conflict with the server copy. Fortunately, though, there are some simple ways to help

Teach you how to synchronize the win10 clipboard with your mobile phone Teach you how to synchronize the win10 clipboard with your mobile phone Jan 06, 2024 am 09:18 AM

A very useful function of win10 clipboard is the cross-device cloud storage function, which is very useful and can help users copy and paste simultaneously on PC devices and mobile devices. The setting method is very simple, just set it on the clipboard in the system. Synchronize win10 clipboard to mobile phone 1. First click Start in the lower left corner to enter settings. 2. Then click "System". 3. Select "Clipboard" on the left. 4. Finally, click Login in "Cross-device synchronization" on the right, and then select your mobile phone.

How to solve the problem that jQuery cannot obtain the form element value How to solve the problem that jQuery cannot obtain the form element value Feb 19, 2024 pm 02:01 PM

To solve the problem that jQuery.val() cannot be used, specific code examples are required. For front-end developers, using jQuery is one of the common operations. Among them, using the .val() method to get or set the value of a form element is a very common operation. However, in some specific cases, the problem of not being able to use the .val() method may arise. This article will introduce some common situations and solutions, and provide specific code examples. Problem Description When using jQuery to develop front-end pages, sometimes you will encounter

How to implement change event binding of select elements in jQuery How to implement change event binding of select elements in jQuery Feb 23, 2024 pm 01:12 PM

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

What are the questions in the Rulong 8 Wine Master exam? What are the questions in the Rulong 8 Wine Master exam? Feb 02, 2024 am 10:18 AM

What are the questions involved in the Yulong 8 Wine Master exam? What is the corresponding answer? How to pass the exam quickly? There are many questions that need to be answered in the Master of Wine Examination activities, and we can refer to the answers to solve them. These questions all involve knowledge of wine. If you need a reference, let’s take a look at the detailed analysis of the answers to the Yakuza 8 Wine Master exam questions! Detailed explanation of answers to questions in the Rulong 8 Wine Master exam 1. Questions about "wine". This is a distilled liquor produced by a distillery established by the royal family. It is brewed from the sugar of sugarcane grown in large quantities in Hawaii. What is the name of this wine? Answer: Rum 2. Question about "wine". The picture shows a drink made from dry ginseng and dry vermouth. It is characterized by the addition of olives and is known as "cockney"

How to solve the problem of the start menu that cannot be used after win11 installation How to solve the problem of the start menu that cannot be used after win11 installation Jan 06, 2024 pm 05:14 PM

Many users have tried to update the win11 system, but found that the start menu cannot be used after the update. This may be because there is a problem with the latest update. We can wait for Microsoft to fix or uninstall these updates to solve the problem. Let's take a look at it together. Solution. What to do if the start menu cannot be used after win11 is installed. Method 1: 1. First open the control panel in win11. 2. Then click the "Uninstall a program" button below the program. 3. Enter the uninstall interface and find "View installed updates" in the upper left corner. 4. After entering, you can view the update time in the update information and uninstall all recent updates. Method 2: 1. In addition, we can also directly download the win11 system without updates. 2. This is a product without the most

Locks and synchronization in Python concurrent programming: keeping your code safe and reliable Locks and synchronization in Python concurrent programming: keeping your code safe and reliable Feb 19, 2024 pm 02:30 PM

Locks and Synchronization in Concurrent Programming In concurrent programming, multiple processes or threads run simultaneously, which can lead to resource contention and inconsistency issues. To solve these problems, locks and synchronization mechanisms are needed to coordinate access to shared resources. Concept of Lock A lock is a mechanism that allows only one thread or process to access a shared resource at a time. When one thread or process acquires a lock, other threads or processes are blocked from accessing the resource until the lock is released. Types of locks There are several types of locks in python: Mutex lock (Mutex): ensures that only one thread or process can access resources at a time. Condition variable: Allows a thread or process to wait for a certain condition and then acquire the lock. Read-write lock: allows multiple threads to read resources at the same time, but only allows one thread to write resources

See all articles