ADO. NET acquires data (DataSet) and the schema instance of the table, ado. netdataset

Source: Internet
Author: User

ADO. NET acquires data (DataSet) and the schema instance of the table, ado. netdataset

The general method for obtaining DataSet through ADO. NET is as follows:

using System.Configuration;using System.Data;using System.Data.SqlClient;public class SQLHelper{ private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString; public static DataSet GetDataSet(string sql) {  using (SqlConnection conn =new SqlConnection(ConnectionString))  {   SqlCommand cmd = new SqlCommand(sql, conn);   SqlDataAdapter adapter = new SqlDataAdapter(cmd);   DataSet ds = new DataSet();   conn.Open();   adapter.Fill(ds);   return ds;  } }}
<?xml version="1.0" encoding="utf-8" ?><configuration> <connectionStrings> <add name="Default" connectionString="Data Source=.;Initial Catalog=EFDb;Integrated Security=true"/> </connectionStrings></configuration>

Here we need to obtain the primary key information of the DataTable. When debugging, we find that there is no primary key information, and the actual database has a primary key (Id)

Set the attribute MissingSchemaAction to System. Data. MissingSchemaAction. AddWithKey for SqlDataAdapter.

The modified SQLHelper is

using System.Configuration;using System.Data;using System.Data.SqlClient;public class SQLHelper{ private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString; public static DataSet GetDataSet(string sql) {  using (SqlConnection conn =new SqlConnection(ConnectionString))  {   SqlCommand cmd = new SqlCommand(sql, conn);   SqlDataAdapter adapter = new SqlDataAdapter(cmd);   adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;   DataSet ds = new DataSet();   conn.Open();   adapter.Fill(ds);   return ds;  } }}

The above architecture example for obtaining data from ADO. NET (DataSet) and tables is all the content shared by Alibaba Cloud. I hope you can give us a reference and support for more customers.

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.