DataTable of ADO. NET object

Source: Internet
Author: User

ADO. NET can process data through DataSet or DataTable objects when disconnected from the database. It can connect to the data source again and update the data source only when data needs to be updated. A DataTable object is a table stored in the local memory. It provides various operations on the row and column data objects in the table. You can directly populate the data from the database to the able object, or add the DataTable object to the existing DataSet object. First, learn about ADO. NET. The following describes how to create a able object. create a able object in either of the following ways: Use the DataTable class constructor to create a DataTable object, for example, DataTable table = new DataTable (); call the Add method of the Tables object of DataSet to create the datasable object DataSet dataset = new dataSet (); DataTable table = dataset. tables. add ("MyTableName"); 2. add a Column in the DataTable object. Call the Add method in the Column of the DataTable object to Add a Column. For example, DataTable table = new DataTable ("table1"); table. columns. add ("name", typeof (System. data. sqlTypes. sqlString); table. columns. add ("Age", typeof (System. data. sqlTypes. sqlInt32); Note: because some data types (such as SqlDateTime, SqlDecimal, and SqlString) in the SQL Server database are different from those in the Common Language Runtime Library (CLR, to save the created table to the SQL Server database, use System. data. the SQL Server data type provided in the SqlType namespace. 3. Create a DataRow object in the DataTable object. Since each row of the DataTable object is a DataRow object, you can use the NewRow method of the DataTable object to create a DataRow object when creating a row, set the data of each column in the new row, and then Add the DataRow object to the table using the Add method. For example, // dt is a DataTable object DataRow row = dt. newRow (); row ["name"] = "Zhang San"; row ["Age"] = 20; dt. rows. add (row); 4 Fill the tables in the SQL Server database into the able object through the Fill method of the DataAdapter object. String connectionString = Properties. settings. default. myDatabaseConnectionString; SqlConnection conn = new SqlConnection (ConnectionString); SqlDataAdapter adapter = new SqlDataAdapter ("Select * from MyTable2", conn); DataSet dataset = new DataSet (); // if no table name is specified, the system automatically uses the default table name adapter. fill (dataset); // you can use the index to generate the table dataGridView1.DataSource = dataset. tables [0]; DataSet object: Create a DataSet object [solution Resource Manager] --> Add a new or existing database [number Data] --> [create a data source] --> when creating or adding an existing database to bind a form control, use the Wizard to create DataSet for DataSet MyDatabase = new DataSet (); after the DataSet object is filled in, you can use the SqlDataAdapter object to import data to the DataSet object. For example, you can use the Fill method to Fill the data in a table in the DataSet object.

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.