Bài vi?t này ch?a Microsoft Visual C#.M?u NET m? ch?ng minh làm th? nào đ? s? d?ng các
SqlDataAdapter đ?i tư?ng đ? c?p nh?t cơ s? d? li?u SQL Server v?i s?a đ?i d? li?u đang ch?y trên m?t
S? li?u đ?i tư?ng là dân cư v?i d? li?u t? m?t b?ng trong cơ s? d? li?u.
Yêu c?u
Danh sách sau v?ch ra đư?c đ? ngh? ph?n c?ng, ph?n m?m, cơ s? h? t?ng m?ng, k? năng và ki?n th?c, và gói d?ch v? đư?c yêu c?u:
- Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, ho?c máy ch? Windows NT 4.0
- Microsoft Visual Studio.NET
- Microsoft Visual C#.NET
- Microsoft SQL Server Phiên b?n 7.0 ho?c cao hơn
Bài vi?t này gi? đ?nh r?ng b?n đ? quen thu?c v?i các ch? đ? sau:
- Visual C#.NET
- ADO.Nguyên t?c cơ b?n NET và cú pháp
Mô t? k? thu?t
Các
SqlDataAdapter đ?i tư?ng ph?c v? như m?t c?u n?i gi?a m?t ADO.NET
S? li?u đ?i tư?ng và cơ s? d? li?u SQL Server.
SqlDataAdapter là m?t đ?i tư?ng trung gian populates m?t ADO.NET
S? li?u đ?i tư?ng v?i d? li?u đó L?y t? cơ s? d? li?u SQL Server, sau đó C?p Nh?t cơ s? d? li?u đ? ph?n ánh nh?ng thay đ?i (ch?ng h?n như chèn, C?p Nh?t và xoá) đư?c th?c hi?n cho d? li?u b?ng cách s? d?ng các
S? li?u đ?i tư?ng.
Các
InsertCommand, các
UpdateCommand, và các
DeleteCommand thu?c tính c?a các
SqlDataAdapter đ?i tư?ng C?p Nh?t cơ s? d? li?u v?i nh?ng s?a đ?i d? li?u đang ch?y trên m?t
S? li?u đ?i tư?ng. Các đ?c tính này
SqlCommand các đ?i tư?ng mà ch? đ?nh CHÈN, C?p Nh?t và các l?nh DELETE Transact-SQL đư?c s? d?ng đ? g?i các s?a đ?i t?p d? li?u cơ s? d? li?u m?c tiêu. Các
SqlCommand các đ?i tư?ng đang đư?c ch? đ?nh cho các thu?c tính này có th? đư?c t?o b?ng tay trong m? ho?c t? đ?ng t?o ra b?ng cách s? d?ng các
SqlCommandBuilder đ?i tư?ng.
M?u m? đ?u tiên trong bài vi?t này ch?ng t? làm th? nào đ? s? d?ng các
SqlCommandBuilder đ?i tư?ng đ? t? đ?ng t?o ra các
UpdateCommand tài s?n c?a các
SqlDataAdapter đ?i tư?ng. M?u th? 2 s? d?ng m?t k?ch b?n mà b?n không th? s? d?ng l?nh t? đ?ng th? h?. M?u th? hai ch?ng t? làm th? nào đ? t? t?o và s? d?ng m?t
SqlCommand đ?i tư?ng là các
UpdateCommand tài s?n c?a m?t
SqlDataAdapter đ?i tư?ng.
T?o m?u SQL Server b?ng
Đ? t?o m?t b?ng SQL Server m?u mà b?n có th? s? d?ng trong các Visual C#.M?u NET m? đ? đư?c di?n t? trong bài vi?t này, h?y làm theo các bư?c sau:
- M? SQL Query Analyzer, và sau đó k?t n?i cơ s? d? li?u mà b?n mu?n t?o ra b?ng m?u. Bài vi?t m?u m? ? đây s? d?ng các Northwind cơ s? d? li?u đư?c bao g?m v?i Microsoft SQL Server.
- Đ? t?o ra m?t b?ng m?u đư?c đ?t tên theo CustTest và chèn m?t b?n ghi vào b?ng, ch?y câu Transact-SQL sau đây:
Create Table CustTest
(
CustID int primary key,
CustName varchar(20)
)
Insert into CustTest values(1,'John')
M?u m? 1: T? đ?ng t?o ra l?nh
N?u nh?ng tuyên b? ch?n mà b?n s? d?ng đ? l?y d? li?u populates m?t
S? li?u d?a trên m?t b?ng cơ s? d? li?u duy nh?t, b?n có th? s? d?ng các
CommandBuilder đ?i tư?ng đ? t? đ?ng t?o ra các
DeleteCommand, các
InsertCommand, và các
UpdateCommand thu?c tính c?a các
DataAdapter. Đi?u này giúp đơn gi?n hoá và làm gi?m m? đó là c?n thi?t đ? th?c hi?n ho?t đ?ng CHÈN, UDPATE và xóa.
Như m?t yêu c?u t?i thi?u, b?n ph?i thi?t l?p các
SelectCommand b?t đ?ng s?n đ? t? đ?ng t?o ra l?nh. Gi?n đ? b?ng mà các
SelectCommand truy xác cú pháp CHÈN, C?p Nh?t và báo cáo xóa t? đ?ng t?o ra.
Các
SelectCommand b?t đ?ng s?n c?ng ph?i tr? l?i ít nh?t m?t khóa chính ho?c c?t duy nh?t. N?u không có là hi?n nay, m?t ngo?i l? InvalidOperation đư?c t?o ra và các l?nh không đư?c t?o ra.
Đ? t?o ra m?t m?u Visual C#.?ng d?ng giao di?n đi?u khi?n NET ch?ng t? làm th? nào đ? s? d?ng các
SqlCommandBuilder đ?i tư?ng đ? t? đ?ng t?o ra các
DeleteCommand, các
InsertCommand, và các
UpdateCommand thu?c tính c?a các
SqlCommand đ?i tư?ng cho m?t
SqlDataAdapter đ?i tư?ng, h?y làm theo các bư?c sau:
- Đ? t?o ra m?t m?i Visual C#.NET ?ng d?ng giao di?n đi?u khi?n, h?y làm theo các bư?c sau:
- B?t đ?u Microsoft Visual Studio.NET.
- Trên các Tệp tr?nh đơn, đi?m đ?n M?i, sau đó b?m Dự án.
- Nh?p vào Visual C# Project dư?i Các lo?i d? án, sau đó b?m Giao di?n đi?u khi?n ?ng d?ng dư?i Khuôn mẫu.
- Thay th? n?i dung m?c đ?nh c?a Class1 v?i đo?n m? sau:
using System.Data;
using System.Data.SqlClient;
using System;
namespace Q308507 {
class Class1 {
static void Main(string[] args) {
SqlConnection cn = new SqlConnection();
DataSet CustomersDataSet = new DataSet();
SqlDataAdapter da;
SqlCommandBuilder cmdBuilder;
//Set the connection string of the SqlConnection object to connect
//to the SQL Server database in which you created the sample
//table.
cn.ConnectionString = "Server=server;Database=northwind;UID=login;PWD=password;";
cn.Open();
//Initialize the SqlDataAdapter object by specifying a Select command
//that retrieves data from the sample table.
da = new SqlDataAdapter("select * from CustTest order by CustId", cn);
//Initialize the SqlCommandBuilder object to automatically generate and initialize
//the UpdateCommand, InsertCommand, and DeleteCommand properties of the SqlDataAdapter.
cmdBuilder = new SqlCommandBuilder(da);
//Populate the DataSet by running the Fill method of the SqlDataAdapter.
da.Fill(CustomersDataSet, "Customers");
//Display the Update, Insert, and Delete commands that were automatically generated
//by the SqlCommandBuilder object.
Console.WriteLine("Update command Generated by the Command Builder : ");
Console.WriteLine("==================================================");
Console.WriteLine(cmdBuilder.GetUpdateCommand().CommandText);
Console.WriteLine(" ");
Console.WriteLine("Insert command Generated by the Command Builder : ");
Console.WriteLine("==================================================");
Console.WriteLine(cmdBuilder.GetInsertCommand().CommandText);
Console.WriteLine(" ");
Console.WriteLine("Delete command Generated by the Command Builder : ");
Console.WriteLine("==================================================");
Console.WriteLine(cmdBuilder.GetDeleteCommand().CommandText);
Console.WriteLine(" ");
//Write out the value in the CustName field before updating the data using the DataSet.
Console.WriteLine("Customer Name before Update : " + CustomersDataSet.Tables["Customers"].Rows[0]["CustName"]);
//Modify the value of the CustName field.
CustomersDataSet.Tables["Customers"].Rows[0]["CustName"] = "Jack";
//Post the data modification to the database.
da.Update(CustomersDataSet, "Customers");
Console.WriteLine("Customer Name updated successfully");
//Close the database connection.
cn.Close();
//Pause
Console.ReadLine();
}
}
} - S?a đ?i các chu?i k?t n?i như là thích h?p cho môi trư?ng c?a b?n.
- Lưu và sau đó ch?y các ?ng d?ng. Nh?n th?y r?ng m?t c?a s? giao di?n đi?u khi?n s? m? ra và sau đó s? hi?n th? đ?u ra sau đây:
Update command Generated by the Command Builder :
==================================================
UPDATE CustTest SET CustID = @p1 , CustName = @p2 WHERE ( CustID = @p3 AND CustName = @p4 )
Insert command Generated by the Command Builder :
==================================================
INSERT INTO CustTest( CustID , CustName ) VALUES ( @p1 , @p2 )
Delete command Generated by the Command Builder :
==================================================
DELETE FROM CustTest WHERE ( CustID = @p1 AND CustName = @p2 )
Customer Name before Update : John
Customer Name updated successfully
- B?m phím b?t k? đ? b? qua c?a s? giao di?n đi?u khi?n và đ? ngăn ch?n các ?ng d?ng.
M?u m? 2: T? t?o và kh?i t?o các tài s?n UpdateCommand
S?n lư?ng mà t?o ra các m? m?u 1 ch? ra r?ng logic đ? t?o ra l?nh t? đ?ng đ? c?p nh?t báo cáo đư?c d?a trên concurrency l?c quan. Đó là, h? sơ không b? khóa đ? ch?nh s?a, và ngư?i dùng ho?c quy tr?nh khác có th? s?a đ?i b?n ghi b?t c? khi nào.
B?i v? m?t h? sơ có th? s?a đ?i sau khi nó đư?c tr? l?i t? b?n tuyên b? ch?n nhưng trư?c khi C?p Nh?t tuyên b? đư?c ban hành, tuyên b? b?n c?p nh?t t? đ?ng t?o ra ch?a m?t WHERE kho?n do đó m?t hàng đư?c C?p Nh?t ch? n?u nó có ch?a t?t c? các giá tr? ban đ?u. Đi?u này là đ? tránh ghi đè d? li?u m?i. N?u m?t tuyên b? b?n c?p nh?t t? đ?ng t?o ra c? g?ng đ? c?p nh?t m?t hàng mà đ? b? xóa ho?c không ch?a các giá tr? ban đ?u đư?c t?m th?y trong các
S? li?u, l?nh không ?nh hư?ng đ?n b?t k? h? sơ, và m?t ngo?i l? DBConcurrencyException đư?c t?o ra. Đ? th? nghi?m này v?i m? trong m? m?u 1, ch?y m? trong Visual Studio Debugger, đ?t m?t breakpoint sau khi các
S? li?u đ? đư?c l?p đ?y nhưng trư?c khi cơ s? d? li?u đư?c C?p Nh?t, và sau đó xóa m?t hàng trong b?ng t? SQL Query Analyzer. Các
Cập nhật g?i sau đó ném ngo?i l?.
N?u b?n mu?n báo cáo C?p Nh?t đ? hoàn thành b?t k? các giá tr? ban đ?u, b?n ph?i thi?t đ?t tư?ng minh các
UpdateCommand Đ?i v?i các
DataAdapter và không d?a vào l?nh t? đ?ng th? h?.
T? t?o và kh?i t?o các
UpdateCommand tài s?n c?a các
SqlDataAdapter đ?i tư?ng đư?c s? d?ng trong m? m?u 1, làm theo các bư?c sau:
- Thay th? m? hi?n có trong các Chính ch?c năng Class1 trong các Visual C#.?ng d?ng giao di?n đi?u khi?n NET b?n đ? t?o ? các M?u m? 1: T? đ?ng t?o ra l?nh ph?n v?i đo?n m? sau:
SqlConnection cn = new SqlConnection();
DataSet CustomersDataSet = new DataSet();
SqlDataAdapter da;
SqlCommand DAUpdateCmd;
cn.ConnectionString = "Server=server;Database=northwind;UID=login;PWD=password;";
cn.Open();
da = new SqlDataAdapter("select * from CustTest order by CustId", cn);
//Initialize the SqlCommand object that will be used as the UpdateCommand for the DataAdapter.
//Note that the WHERE clause uses only the CustId field to locate the record to be updated.
DAUpdateCmd = new SqlCommand("Update CustTest set CustName = @pCustName where CustId = @pCustId", da.SelectCommand.Connection);
//Create and append the parameters for the Update command.
DAUpdateCmd.Parameters.Add(new SqlParameter("@pCustName", SqlDbType.VarChar));
DAUpdateCmd.Parameters["@pCustName"].SourceVersion = DataRowVersion.Current;
DAUpdateCmd.Parameters["@pCustName"].SourceColumn = "CustName";
DAUpdateCmd.Parameters.Add(new SqlParameter("@pCustId", SqlDbType.Int));
DAUpdateCmd.Parameters["@pCustId"].SourceVersion = DataRowVersion.Original;
DAUpdateCmd.Parameters["@pCustId"].SourceColumn = "CustId";
//Assign the SqlCommand to the UpdateCommand property of the SqlDataAdapter.
da.UpdateCommand = DAUpdateCmd;
da.Fill(CustomersDataSet, "Customers");
Console.WriteLine("Customer Name before Update : " + CustomersDataSet.Tables["Customers"].Rows[0]["CustName"]);
CustomersDataSet.Tables["Customers"].Rows[0]["CustName"] = "Jack";
da.Update(CustomersDataSet, "Customers");
Console.WriteLine("Customer Name updated successfully");
cn.Close();
Console.ReadLine();
- S?a đ?i các chu?i k?t n?i như là thích h?p cho môi trư?ng c?a b?n.
- L?p l?i các bư?c 1 thông qua 4 trong các M?u m? 1: T? đ?ng t?o ra l?nh keá tieáp. Lưu ? r?ng m?t ngo?i l? DBConcurrencyException không c?n đư?c t?o ra.
ID c?a bài: 308507 - L?n xem xét sau cùng: 27 Tháng Tám 2011 - Xem xét l?i: 2.0
Áp d?ng
- Microsoft ADO.NET 1.1
- Microsoft Visual C# .NET 2002 Standard Edition
- Microsoft Visual C# .NET 2003 Standard Edition
| kbhowtomaster kbsqlclient kbsystemdata kbmt KB308507 KbMtvi |
Máy d?chQUAN TRỌNG: Bài vi?t này đư?c d?ch b?ng ph?n m?m d?ch máy c?a Microsoft ch? không ph?i do con ngư?i d?ch. Microsoft cung c?p các bài vi?t do con ngư?i d?ch và c? các bài vi?t do máy d?ch đ? b?n có th? truy c?p vào t?t c? các bài vi?t trong Cơ s? Ki?n th?c c?a chúng tôi b?ng ngôn ng? c?a b?n. Tuy nhiên, bài vi?t do máy d?ch không ph?i lúc nào c?ng hoàn h?o. Lo?i bài vi?t này có th? ch?a các sai sót v? t? v?ng, cú pháp ho?c ng? pháp, gi?ng như m?t ngư?i nư?c ngoài có th? m?c sai sót khi nói ngôn ng? c?a b?n. Microsoft không ch?u trách nhi?m v? b?t k? s? thi?u chính xác, sai sót ho?c thi?t h?i nào do vi?c d?ch sai n?i dung ho?c do ho?t đ?ng s? d?ng c?a khách hàng gây ra. Microsoft c?ng thư?ng xuyên c?p nh?t ph?n m?m d?ch máy này.
Nh?p chu?t vào đây đ? xem b?n ti?ng Anh c?a bài vi?t này:
308507
(http://support.microsoft.com/kb/308507/en-us/
)