C#顯示DataTable指定行的記錄屬性值
來源:易賢網(wǎng) 閱讀:3397 次 日期:2014-08-20 15:00:26
溫馨提示:易賢網(wǎng)小編為您整理了“C#顯示DataTable指定行的記錄屬性值”,方便廣大網(wǎng)友查閱!

C#顯示指定行的記錄屬性值,用DataTable中的NewRow()方法生成新的DataRow, 對新的DataRow的DataColumn對象設置數(shù)值,將新的DataRow加進DataTable中,創(chuàng)建DataSet 并調用mySqlDataAdapter的fill方法,從myDataSet中獲取Customers表,具體代碼如下:

view sourceprint?01using System;

02using System.Data;

03using System.Data.SqlClient;

04namespace ModifyDatabase

05{

06 public class ModifyDatabase

07 {

08 // 顯示指定行的記錄屬性值

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 // 用DataTable中的NewRow()方法生成新的DataRow

29 Console.WriteLine("Calling myDataTable.NewRow()");

30 DataRow myNewDataRow = myDataTable.NewRow();

31 Console.WriteLine("myNewDataRow.RowState = " +

32 myNewDataRow.RowState);

33 // 對新的DataRow的DataColumn對象設置數(shù)值

34 myNewDataRow["CustomerID"] = "JxCOM";

35 myNewDataRow["CompanyName"] = "Jx Company";

36 myNewDataRow["Address"] = "1 Main Street";

37 // 將新的DataRow加進DataTable中

38 Console.WriteLine("Calling myDataTable.Rows.Add()");

39 myDataTable.Rows.Add(myNewDataRow);

40 Console.WriteLine("myNewDataRow.RowState = " +

41 myNewDataRow.RowState);

42 // 將新行推進數(shù)據(jù)庫中

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 // 創(chuàng)建SELECT語句得SqlCommand對象

59 SqlCommand mySelectCommand = mySqlConnection.CreateCommand();

60 mySelectCommand.CommandText =

61 "SELECT CustomerID, CompanyName, Address " +

62 "FROM Customers " +

63 "ORDER BY CustomerID";

64 // 創(chuàng)建INSERT語句得SqlCommand對象

65 SqlCommand myInsertCommand = mySqlConnection.CreateCommand();

66 myInsertCommand.CommandText =

67 "INSERT INTO Customers (" +

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 // 創(chuàng)建SqlDataAdapter并設置相應屬性

79 SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();

80 mySqlDataAdapter.SelectCommand = mySelectCommand;

81 mySqlDataAdapter.InsertCommand = myInsertCommand;

82 // 創(chuàng)建DataSet 并調用mySqlDataAdapter的fill方法

83 DataSet myDataSet = new DataSet();

84 Console.WriteLine("Calling mySqlDataAdapter.Fill()");

85 mySqlConnection.Open();

86 int numOfRows =

87 mySqlDataAdapter.Fill(myDataSet, "Customers");

88 mySqlConnection.Close();

89 Console.WriteLine("numOfRows = " + numOfRows);

90 // 從myDataSet中獲取Customers表

91 DataTable customersDataTable = myDataSet.Tables["Customers"];

92 // 在Customers表中添加新行

93 AddDataRow(customersDataTable, mySqlDataAdapter,

94 mySqlConnection);

95 }

96 }

97}

更多信息請查看IT技術專欄

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:C#顯示DataTable指定行的記錄屬性值

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權所有:易賢網(wǎng)