C # display the record attribute values of a specified row. Use the NewRow () method in the able to generate a new DataRow, set a value for the DataColumn object of the new DataRow, and add the new DataRow to the DataTable, create a DataSet and call the fill method of mySqlDataAdapter to obtain the MERs table from myDataSet. The Code is as follows:
View sourceprint? 01 using System;
02 using System. Data;
03 using System. Data. SqlClient;
04 namespace ModifyDatabase
05 {
06 public class ModifyDatabase
07 {
08 // display the record property value of a specified row
09 public static void DisplayDataRow (
10 DataRow myDataRow,
11 DataTable myDataTable
12)
13 {
14 Console. WriteLine ("\ nIn DisplayDataRow ()");
15 foreach (DataColumn myDataColumn in myDataTable. Columns)
16 {
17 Console. WriteLine (myDataColumn + "=" +
18 myDataRow [myDataColumn]);
19}
20}
21 public static void AddDataRow (
22 DataTable myDataTable,
23 SqlDataAdapter mySqlDataAdapter,
24 SqlConnection mySqlConnection
25)
26 {
27 Console. WriteLine ("\ nIn AddDataRow ()");
28 // use the NewRow () method in the DataTable to generate a new DataRow
29 Console. WriteLine ("Calling myDataTable. NewRow ()");
30 DataRow myNewDataRow = myDataTable. NewRow ();
31 Console. WriteLine ("myNewDataRow. RowState =" +
32 myNewDataRow. RowState );
33 // set a value for the new DataRow DataColumn object
34 myNewDataRow ["CustomerID"] = "JxCOM ";
35 myNewDataRow ["CompanyName"] = "Jx Company ";
36 myNewDataRow ["Address"] = "1 Main Street ";
37 // Add the new DataRow to the able
38 Console. WriteLine ("Calling myDataTable. Rows. Add ()");
39 myDataTable. Rows. Add (myNewDataRow );
40 Console. WriteLine ("myNewDataRow. RowState =" +
41 myNewDataRow. RowState );
42 // push the new row into the database
43 Console. WriteLine ("Calling mySqlDataAdapter. Update ()");
44 mySqlConnection. Open ();
45 int numOfRows = mySqlDataAdapter. Update (myDataTable );
46 mySqlConnection. Close ();
47 Console. WriteLine ("numOfRows =" + numOfRows );
48 Console. WriteLine ("myNewDataRow. RowState =" +
49 myNewDataRow. RowState );
50 DisplayDataRow (myNewDataRow, myDataTable );
51}
52 public static void Main ()
53 {
54 SqlConnection mySqlConnection =
55 new SqlConnection (
56 "server = localhost; database = Northwind; uid = sa; pwd = sa"
57 );
58 // create the SqlCommand object for the SELECT statement
59 SqlCommand mySelectCommand = mySqlConnection. CreateCommand ();
60 mySelectCommand. CommandText =
61 "SELECT CustomerID, CompanyName, Address" +
62 "FROM MERs" +
63 "order by CustomerID ";
64 // create the SqlCommand object for the INSERT statement
65 SqlCommand myInsertCommand = mySqlConnection. CreateCommand ();
66 myInsertCommand. CommandText =
67 "insert into MERs (" +
68 "CustomerID, CompanyName, Address" +
69 ") VALUES (" +
70 "@ CustomerID, @ CompanyName, @ Address" +
71 ")";
72 myInsertCommand. Parameters. Add ("@ CustomerID", SqlDbType. NChar,
73 5, "CustomerID ");
74 myInsertCommand. Parameters. Add ("@ CompanyName", SqlDbType. NVarChar,
75 40, "CompanyName ");
76 myInsertCommand. Parameters. Add ("@ Address", SqlDbType. NVarChar,
77 60, "Address ");
78 // create a SqlDataAdapter and set the corresponding attributes
79 SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter ();
80 mySqlDataAdapter. SelectCommand = mySelectCommand;
81 mySqlDataAdapter. InsertCommand = myInsertCommand;
82 // create a DataSet and call the fill method of mySqlDataAdapter
83 DataSet myDataSet = new DataSet ();
84 Console. WriteLine ("Calling mySqlDataAdapter. Fill ()");
85 mySqlConnection. Open ();
86 int numOfRows =
87 mySqlDataAdapter. Fill (myDataSet, "MERs ");
88 mySqlConnection. Close ();
89 Console. WriteLine ("numOfRows =" + numOfRows );
90 // obtain the MERs table from myDataSet
91 DataTable customersDataTable = myDataSet. Tables ["MERs"];
92 // Add a new row to the MERs table
93 AddDataRow (customersDataTable, mySqlDataAdapter,
94 mySqlConnection );
95}
96}
97}