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.