C#显示DataTable指定行的记录属性值
来源:易贤网 阅读:3332 次 日期:2014-08-20 15:00:26
温馨提示:易贤网小编为您整理了“C#显示DataTable指定行的记录属性值”,方便广大网友查阅!

C#显示指定行的记录属性值,用DataTable中的NewRow()方法生成新的DataRow, 对新的DataRow的DataColumn对象设置数值,将新的DataRow加进DataTable中,创建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对象设置数值

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 // 将新行推进数据库中

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 // 创建SELECT语句得SqlCommand对象

59 SqlCommand mySelectCommand = mySqlConnection.CreateCommand();

60 mySelectCommand.CommandText =

61 "SELECT CustomerID, CompanyName, Address " +

62 "FROM Customers " +

63 "ORDER BY CustomerID";

64 // 创建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 // 创建SqlDataAdapter并设置相应属性

79 SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();

80 mySqlDataAdapter.SelectCommand = mySelectCommand;

81 mySqlDataAdapter.InsertCommand = myInsertCommand;

82 // 创建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技术专栏

更多信息请查看网络编程
易贤网手机网站地址:C#显示DataTable指定行的记录属性值
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标