. Net queries multiple tables at a time, fills in dataset, and specifies the table name (Dataset specifies the datatable name)

Source: Internet
Author: User

In actual scenarios, you may need to query the table sheets in the database at a time,

Fill the dataset with the fill method of the sqldataadapter class, and specify the name of the able table filled in the dataset,

For example, set it to the same as the database.


You can use the tablemappings attribute of the sqldataadapter class.

Fill in the table name in dataset with the fill method of the sqldataadapter class. The default value is table, Table1, Table2 ......


Sqlconnection conn = new sqlconnection ("Server =.; database = databasename; uid = sa; Pwd = sa"); // you can define a database connection.

String SQL = "select * from user; select * from product; select * from article ;";

Sqlcommand cmd = new sqlcommand (SQL, Conn );

Sqldataadapter adapter = new sqldataadapter (CMD );

// Specify the table name, tablemappings. Add method. The first parameter is the default table name for fill dataset, and the second parameter is the specified table name, which can be customized as needed.

Adapter. tablemappings. Add ("table", "user ");

Adapter. tablemappings. Add ("Table1", "product ");

Adapter. tablemappings. Add ("Table2", "article ");

Dataset DS = new dataset ();

Adapter. Fill (DS );
// There are three tables filled in the dataset: User, product, and article.

In this way,

It is easy to associate multiple tables (tables filled with dataset) returned by the query with the tables in the database for convenient use in the program.


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.