บทความนี้ประกอบด้วยตัวอย่างรหัส Microsoft Visual c# .NET ที่แสดงให้เห็นถึงวิธีการใช้การ
SqlDataAdapterวัตถุการปรับปรุงฐานข้อมูล SQL Server ด้วยการปรับเปลี่ยนข้อมูลที่ทำงานบนตัว
ชุดข้อมูลวัตถุที่มีการเติมข้อมูลให้กับข้อมูลจากตารางในฐานข้อมูล
ความต้องการ
รายการต่อไปนี้แสดงฮาร์ดแวร์ที่แนะนำ ซอฟต์แวร์ โครงสร้างพื้นฐานของเครือข่าย ทักษะ และทราบ และเซอร์วิสแพ็คที่จำเป็นต้องใช้:
- Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 ขั้นสูงเซิร์ฟเวอร์ หรือเซิร์ฟเวอร์ Windows NT 4.0
- Microsoft Visual Studio .NET
- Microsoft Visual c# .NET
- Microsoft SQL Server รุ่น 7.0 หรือรุ่นใหม่กว่า
บทความนี้อนุมานว่า คุณไม่คุ้นเคยกับหัวข้อต่อไปนี้:
- ภาพ.NET c#
- ข้อมูลพื้นฐานของเดสก์ท็อป ADO.NET และไวยากรณ์
คำอธิบายเกี่ยวกับเทคนิค
กระบวนการ
SqlDataAdapterวัตถุที่ทำหน้าที่เป็นบริดจ์ระหว่างการ ADO.NET
ชุดข้อมูลวัตถุและฐานข้อมูล SQL Server
SqlDataAdapterมีวัตถุที่ populates ADO.NET เป็นสื่อกลาง
ชุดข้อมูลวัตถุที่ มีข้อมูลที่ดึงมาจากฐานข้อมูล SQL Server จากฐานข้อมูลเพื่อให้สะท้อนถึงการเปลี่ยนแปลง (เช่นแทรก ปรับปรุง และลบ) ที่เกิดขึ้นกับข้อมูล โดยใช้การปรับปรุง นี้
ชุดข้อมูลวัตถุ
กระบวนการ
InsertCommandกระบวนการ
UpdateCommandและ
DeleteCommandคุณสมบัติของ
SqlDataAdapterวัตถุที่ปรับปรุงฐานข้อมูล ด้วยการปรับเปลี่ยนข้อมูลที่เรียกใช้บนตัว
ชุดข้อมูลวัตถุ คุณสมบัติเหล่านี้ได้
SqlCommandวัตถุที่ระบุ INSERT, UPDATE และคำสั่ง DELETE Transact-SQL ที่ใช้ในการลงรายการบัญชีการปรับเปลี่ยนชุดข้อมูลในฐานข้อมูลเป้าหมาย กระบวนการ
SqlCommandวัตถุที่ได้รับการกำหนดคุณสมบัติเหล่านี้สามารถถูกสร้างขึ้นด้วยตนเองในรหัส หรือสร้างขึ้นโดยอัตโนมัติ โดยใช้การ
SqlCommandBuilderวัตถุ
ตัวอย่างรหัสแรกในบทความนี้อธิบายวิธีใช้นี้
SqlCommandBuilderวัตถุที่สร้างโดยอัตโนมัติ
UpdateCommandคุณสมบัตินี้
SqlDataAdapterวัตถุ ตัวอย่างที่สองใช้ในสถานการณ์ที่คุณไม่สามารถใช้การสร้างคำสั่งโดยอัตโนมัติ วิธีการสร้างด้วยตนเอง และใช้แสดงตัวอย่างสองตัว
SqlCommandวัตถุเป็นแบบ
UpdateCommandคุณสมบัติของคำ
SqlDataAdapterวัตถุ
สร้างตารางเซิร์ฟเวอร์ SQL ตัวอย่าง
เมื่อต้องการสร้างตาราง SQL Server ตัวอย่างที่คุณสามารถใช้ในตัวอย่างรหัส.NET c# แสดงผลที่ได้รับการบันทึกไว้ในบทความนี้ ดำเนินการดังต่อไปนี้:
- เปิดตัววิเคราะห์คำถาม SQL และการเชื่อมต่อกับฐานข้อมูลที่คุณต้องการสร้างตารางตัวอย่าง ตัวอย่างรหัสในนี้การใช้บทความนี้northwindฐานข้อมูลที่มาพร้อมกับ Microsoft SQL Server
- เมื่อต้อง การสร้างตารางตัวอย่างที่ชื่อ CustTest และแทรกเรกคอร์ดในตาราง เรียกใช้คำสั่ง Transact SQL ต่อไปนี้:
Create Table CustTest
(
CustID int primary key,
CustName varchar(20)
)
Insert into CustTest values(1,'John')
ตัวอย่างรหัส 1: คำสั่งที่สร้างโดยอัตโนมัติ
ถ้าคำสั่ง SELECT ที่คุณใช้ในการดึงข้อมูลที่ populates เป็น
ชุดข้อมูลใช้ในตารางฐานข้อมูลเดียว คุณสามารถใช้การ
CommandBuilderวัตถุที่สร้างโดยอัตโนมัติ
DeleteCommandกระบวนการ
InsertCommandและ
UpdateCommandคุณสมบัติของ
DataAdapter. simplifies และลดรหัสที่จำเป็นต้องใช้เพื่อทำการดำเนินงาน INSERT, UDPATE และลบ
เป็นความต้องการต่ำสุด คุณต้องตั้งค่านี้
SelectCommandคุณสมบัติเพื่อสร้างคำสั่งโดยอัตโนมัติ schema ของตารางที่จะ
SelectCommandดึงกำหนดไวยากรณ์ของ INSERT, UPDATE และการลบรายการที่ถูกสร้างขึ้นโดยอัตโนมัติ
กระบวนการ
SelectCommandคุณสมบัติต้องส่งคืนนอกจากนี้คีย์หลักอย่างน้อยหนึ่งหรือคอลัมน์ที่ไม่ซ้ำกัน ถ้าไม่มีอยู่ มีสร้างข้อยกเว้น InvalidOperation และจะไม่มีสร้างคำสั่ง
เมื่อต้องการสร้างโปรแกรมประยุกต์คอนโซล Visual .NET c# ตัวอย่างที่แสดงวิธีใช้นี้
SqlCommandBuilderวัตถุที่สร้างโดยอัตโนมัติ
DeleteCommandกระบวนการ
InsertCommandและ
UpdateCommandคุณสมบัติของ
SqlCommandวัตถุสำหรับคำ
SqlDataAdapterวัตถุ ทำตามขั้นตอนเหล่านี้:
- เมื่อต้องการสร้างโปรแกรมประยุกต์.NET c# Visual คอนโซลใหม่ ดำเนินการดังต่อไปนี้:
- เริ่ม Microsoft Visual Studio .NET
- ในการแฟ้ม:เมนู ให้ชี้ไปที่ใหม่แล้ว คลิกProject.
- คลิกแสดงโครงการ c#ภายใต้ชนิดโครงการแล้ว คลิกแอพลิเคชันของคอนโซลภายใต้แม่แบบ.
- แทนเนื้อหาเริ่มต้นของ Class1 กับรหัสต่อไปนี้:
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();
}
}
} - แก้ไขสายอักขระการเชื่อมต่อตามความเหมาะสมสำหรับสภาพแวดล้อมการทำงานของคุณ
- บันทึก และเรียกใช้โปรแกรมประยุกต์ โปรดสังเกตว่า หน้าต่างคอนโซลเปิด และแสดงผลลัพธ์ต่อไปนี้:
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
- กดแป้นใด ๆ เพื่อหน้าต่างคอนโซลที่ยกเลิก และหยุดการแอพลิเคชัน
ตัวอย่างรหัส 2: สร้างด้วยตนเอง และการเตรียมใช้งานคุณสมบัติ UpdateCommand
ผลลัพธ์ที่สร้างรหัสตัวอย่าง 1 บ่งชี้ว่า ตรรกะเพื่อสร้างคำสั่งโดยอัตโนมัติสำหรับใบแจ้งยอด UPDATE จะขึ้นอยู่กับการเกิดพร้อมกัน optimistic นั่นคือ เร็กคอร์ดที่ไม่ได้ถูกล็อกเพื่อแก้ไข และผู้ใช้อื่นหรือกระบวนสามารถแก้ไขเร็กคอร์ดทุกครั้งที่
เนื่องจากระเบียนสามารถถูกปรับเปลี่ยนหลังจากที่มีการส่งคืนจากคำสั่ง SELECT แต่ก่อน UPDATE ใน การออกใบแจ้งยอด งบ UPDATE ที่ถูกสร้างขึ้นโดยอัตโนมัติประกอบด้วยอนุประโยคเพื่อให้แถวถูกปรับปรุงเฉพาะเมื่อประกอบด้วยค่าเริ่มต้นทั้งหมด นี่คือการหลีกเลี่ยงการเขียนทับข้อมูลใหม่ If an UPDATE statement that is automatically generated tries to update a row that has been deleted or does not contain the original values that are found in the
ชุดข้อมูล, the command does not affect any records, and a DBConcurrencyException exception is generated. To test this with the code in Code Sample 1, run the code in the Visual Studio Debugger, set a breakpoint after the
ชุดข้อมูลhas been filled but before the database is updated, and then delete the one row in the table from SQL Query Analyzer. กระบวนการ
การปรับปรุงcall then throws the exception.
If you want the UPDATE statement to complete regardless of original values, you must explicitly set the
UpdateCommandสำหรับการ
DataAdapterand not rely on automatic command generation.
To manually create and initialize the
UpdateCommandคุณสมบัตินี้
SqlDataAdapterobject that is used in Code Sample 1, follow these steps:
- Replace the existing code in theหลักfunction of Class1 in the Visual C# .NET console application that you created in theCode Sample 1: Automatically Generated Commandssection with the following code:
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();
- Modify the connection string as appropriate for your environment.
- Repeat steps 1 through 4 in theCode Sample 1: Automatically Generated Commandsส่วน Note that a DBConcurrencyException exception is no longer generated.
หมายเลขบทความ (Article ID): 308507 - รีวิวครั้งสุดท้าย: 10 มกราคม 2554 - Revision: 4.0
ใช้กับ
- Microsoft ADO.NET 1.1
- Microsoft Visual C# .NET 2002 Standard Edition
- Microsoft Visual C# .NET 2003 Standard Edition
| kbhowtomaster kbsqlclient kbsystemdata kbmt KB308507 KbMtth |
แปลโดยคอมพิวเตอร์ข้อมูลสำคัญ: บทความนี้แปลโดยซอฟต์แวร์การแปลด้วยคอมพิวเตอร์ของ Microsoft แทนที่จะเป็นนักแปลที่เป็นบุคคล Microsoft มีบทความที่แปลโดยนักแปลและบทความที่แปลด้วยคอมพิวเตอร์ เพื่อให้คุณสามารถเข้าถึงบทความทั้งหมดในฐานความรู้ของเรา ในภาษาของคุณเอง อย่างไรก็ตาม บทความที่แปลด้วยคอมพิวเตอร์นั้นอาจมีข้อบกพร่อง โดยอาจมีข้อผิดพลาดในคำศัพท์ รูปแบบการใช้ภาษาและไวยากรณ์ เช่นเดียวกับกรณีที่ชาวต่างชาติพูดผิดเมื่อพูดภาษาของคุณ Microsoft ไม่มีส่วนรับผิดชอบต่อความคลาดเคลื่อน ความผิดพลาดหรือความเสียหายที่เกิดจากการแปลเนื้อหาผิดพลาด หรือการใช้บทแปลของลูกค้า และ Microsoft มีการปรับปรุงซอฟต์แวร์การแปลด้วยคอมพิวเตอร์อยู่เป็นประจำ
ต่อไปนี้เป็นฉบับภาษาอังกฤษของบทความนี้:
308507
(http://support.microsoft.com/kb/308507/en-us/
)