您好,登錄后才能下訂單哦!
這篇文章主要介紹ADO.NET如何獲取數(shù)據(jù)同時(shí)獲取表的架構(gòu),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
普通的ADO.NET獲取DataSet的寫(xiě)法如下:
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>
這里需要獲取DataTable的主鍵信息,調(diào)試時(shí)發(fā)現(xiàn)沒(méi)有主鍵信息,而實(shí)際數(shù)據(jù)庫(kù)中是有主鍵(Id)
此時(shí)需要給SqlDataAdapter設(shè)置屬性MissingSchemaAction為System.Data.MissingSchemaAction.AddWithKey
修改后的SQLHelper為
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; } } }
以上是“ADO.NET如何獲取數(shù)據(jù)同時(shí)獲取表的架構(gòu)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。