C # Displays the record property values for the DataTable-specified row

Source: Internet
Author: User

C # Displays the value of the record property for the specified row, generates a new DataRow with the NewRow () method in the DataTable, sets a value for the new DataRow DataColumn object, adds a new DataRow into the DataTable, creates a dataset and call the Mysqldataadapter fill method, get the Customers table from the myDataSet, the specific code is as follows:

View Sourceprint?01using System;

02using System.Data;

03using System.Data.SqlClient;

04namespace Modifydatabase

259

Modifydatabase public class

07 {

08//Display the Record property value for the specified row

The public static void Displaydatarow (

Ten DataRow MyDataRow,

One DataTable mydatatable

12)

13 {

Console.WriteLine ("NIn Displaydatarow ()");

foreach (DataColumn mydatacolumn in Mydatatable.columns)

16 {

Console.WriteLine (mydatacolumn + "=" +

Mydatarow[mydatacolumn]);

19}

20}

public static void Adddatarow (

DataTable mydatatable,

SqlDataAdapter Mysqldataadapter,

SqlConnection mysqlconnection

25)

26 {

Console.WriteLine ("NIn Adddatarow ()");

28//using the NewRow () method in the DataTable to generate a new DataRow

Console.WriteLine ("Calling Mydatatable.newrow ()");

DataRow Mynewdatarow = Mydatatable.newrow ();

Console.WriteLine ("Mynewdatarow.rowstate =" +

Mynewdatarow.rowstate);

33//Set numeric values for new DataRow DataColumn objects

mynewdatarow["CustomerID"] = "jxcom";

mynewdatarow["CompanyName"] = "Jx Company";

mynewdatarow["Address"] = "1 Main Street";

37//Add new DataRow to the DataTable

Console.WriteLine ("Calling MyDataTable.Rows.Add ()");

MYDATATABLE.ROWS.ADD (Mynewdatarow);

Console.WriteLine ("Mynewdatarow.rowstate =" +

Mynewdatarow.rowstate);

42//Push the new line into the database

Console.WriteLine ("Calling Mysqldataadapter.update ()");

Mysqlconnection.open ();

the int numofrows = Mysqldataadapter.update (mydatatable);

Mysqlconnection.close ();

Console.WriteLine ("numofrows =" + numofrows);

Console.WriteLine ("Mynewdatarow.rowstate =" +

Mynewdatarow.rowstate);

Displaydatarow (Mynewdatarow, mydatatable);

51}

The public static void Main ()

53 {

SqlConnection mysqlconnection =

New SqlConnection (

"Server=localhost;database=northwind;uid=sa;pwd=sa"

57);

58//SqlCommand object to create SELECT statement

SqlCommand Myselectcommand = Mysqlconnection.createcommand ();

Myselectcommand.commandtext =

"Select CustomerID, CompanyName, address" +

"From Customers" +

"Order by CustomerID";

64//Create INSERT statement SqlCommand object

SqlCommand Myinsertcommand = Mysqlconnection.createcommand ();

Myinsertcommand.commandtext =

"INSERT into Customers (" +

"CustomerID, CompanyName, address" +

") VALUES (" +

"@CustomerID, @CompanyName, @Address" +

71 ")";

MYINSERTCOMMAND.PARAMETERS.ADD ("@CustomerID", SqlDbType.NChar,

5, "CustomerID");

MYINSERTCOMMAND.PARAMETERS.ADD ("@CompanyName", SqlDbType.NVarChar,

, "CompanyName");

MYINSERTCOMMAND.PARAMETERS.ADD ("@Address", SqlDbType.NVarChar,

, "address");

78//Create SqlDataAdapter and set appropriate properties

SqlDataAdapter mysqldataadapter = new SqlDataAdapter ();

Mysqldataadapter.selectcommand = Myselectcommand;

Bayi Mysqldataadapter.insertcommand = Myinsertcommand;

82//Create DataSet and Invoke Mysqldataadapter Fill method

The DataSet myDataSet = new DataSet ();

Console.WriteLine ("Calling Mysqldataadapter.fill ()");

Mysqlconnection.open ();

int numofrows =

Mysqldataadapter.fill (myDataSet, "Customers");

Mysqlconnection.close ();

Console.WriteLine ("numofrows =" + numofrows);

90//Get Customers table from myDataSet

mydataset.tables[DataTable customersdatatable = "Customers"];

92///Add a new row to the Customers table

Adddatarow (CustomersDataTable, Mysqldataadapter,

Mysqlconnection);

95}

96}

773

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.