1using System.Data;
2using System.Data.SqlClient;
3using System;
4namespace Q308507 {
5
6 class Class1 {
7 static void Main(string[] args) {
8
9 SqlConnection cn = new SqlConnection();
10 DataSet CustomersDataSet = new DataSet();
11 SqlDataAdapter da;
12 SqlCommandBuilder cmdBuilder;
13
14 //Set the connection string of the SqlConnection object to connect
15 //to the SQL Server database in which you created the sample
16 //table.
17 cn.ConnectionString = "Server=server;Database=northwind;UID=login;PWD=password;";
18
19 cn.Open();
20
21 //Initialize the SqlDataAdapter object by specifying a Select command
22 //that retrieves data from the sample table.
23 da = new SqlDataAdapter("select * from CustTest order by CustId", cn);
24
25 //Initialize the SqlCommandBuilder object to automatically generate and initialize
26 //the UpdateCommand, InsertCommand, and DeleteCommand properties of the SqlDataAdapter.
27 cmdBuilder = new SqlCommandBuilder(da);
28
29 //Populate the DataSet by running the Fill method of the SqlDataAdapter.
30 da.Fill(CustomersDataSet, "Customers");
31
32 //Display the Update, Insert, and Delete commands that were automatically generated
33 //by the SqlCommandBuilder object.
34 Console.WriteLine("Update command Generated by the Command Builder : ");
35 Console.WriteLine("==================================================");
36 Console.WriteLine(cmdBuilder.GetUpdateCommand().CommandText);
37 Console.WriteLine(" ");
38
39 Console.WriteLine("Insert command Generated by the Command Builder : ");
40 Console.WriteLine("==================================================");
41 Console.WriteLine(cmdBuilder.GetInsertCommand().CommandText);
42 Console.WriteLine(" ");
43
44 Console.WriteLine("Delete command Generated by the Command Builder : ");
45 Console.WriteLine("==================================================");
46 Console.WriteLine(cmdBuilder.GetDeleteCommand().CommandText);
47 Console.WriteLine(" ");
48
49 //Write out the value in the CustName field before updating the data using the DataSet.
50 Console.WriteLine("Customer Name before Update : " + CustomersDataSet.Tables["Customers"].Rows[0]["CustName"]);
51
52 //Modify the value of the CustName field.
53 CustomersDataSet.Tables["Customers"].Rows[0]["CustName"] = "Jack";
54
55 //Post the data modification to the database.
56 da.Update(CustomersDataSet, "Customers");
57
58 Console.WriteLine("Customer Name updated successfully");
59
60 //Close the database connection.
61 cn.Close();
62
63 //Pause
64 Console.ReadLine();
65 }
66 }
67
68}