普通的ADO.NET获取DataSet的写法如下:
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; } } }
<"1.0" encoding="utf-8" "Default" connectionString="Data Source=.;Initial Catalog=EFDb;Integrated Security=true"/> </connectionStrings> </configuration>
这里需要获取DataTable的主键信息,调试时发现没有主键信息,而实际数据库中是有主键(Id)
此时需要给SqlDataAdapter设置属性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获取数据(DataSet)同时获取表的架构实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“ADO.NET获取数据(DataSet)同时获取表的架构实例”评论...