1,
System.Data.EntityClient 命名空间是 实体框架的 .NET Framework 数据提供程序。EntityClient 提供程序使用存储特定的 ADO.NET 数据提供程序类和映射元数据与实体数据模型进行交互。EntityClient 首先将对概念性实体执行的操作转换为对物理数据源执行的操作。然后再将物理数据源返回的结果集转换为概念性实体。
代码如下:
View Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq; using System.Xml.Linq; using System.Data.Objects; using System.Data.Common; using System.Data.EntityClient; using System.Data; using System.Configuration; namespace Console1 { class Program { static void Main(string[] args) { #region EntityConnection连接对象 //string con = "name = studentEntities"; //连接字符串 ////连接字符串也可以这样写 ////string con = System.Configuration.ConfigurationManager.ConnectionStrings["studentEntities"].ConnectionString; //using (EntityConnection econn = new EntityConnection(con))//构造连接对象 //{ // string esql = "select value c from studentEntities.T_StuInfo as c where c.stuid < 5"; // econn.Open();//建立与数据源的连接 // EntityCommand ecmd = new EntityCommand(esql, econn);//构造命令对象 // EntityDataReader ereader = ecmd.ExecuteReader(CommandBehavior.SequentialAccess); // while (ereader.Read()) //如果有数据,该函数会读取一条数据 // { // Console.WriteLine(ereader["name"]); // } // Console.WriteLine(ecmd.ToTraceString()); //} #endregion #region EntityCommand命令对象 //string con = "name=studentEntities"; //using (EntityConnection econn = new EntityConnection(con)) //{ // string esql = "select value c from studentEntities.T_StuInfo as c where c.stuid=4";//e sql语句 // econn.Open();//打开连接对象 // EntityCommand ecmd = econn.CreateCommand();//使用对象创建一个命令 // ecmd.CommandText = esql; //命令语句 // EntityDataReader ereader = ecmd.ExecuteReader(CommandBehavior.SequentialAccess); // if (ereader.Read()) // { // Console.WriteLine(ereader["name"]); // } // Console.WriteLine(ecmd.ToTraceString()); //} #endregion #region EntityConnectionStringBuilder 创建和连接字符串 //EntityConnectionStringBuilder esb = new EntityConnectionStringBuilder(); //esb.Provider = "System.Data.SqlClient";//数据提供程序名字 //esb.Metadata =@"res://*/Model2.csdl|res://*/Model2.ssdl|res://*/Model2.msl"; //连接字符串的元数据位置 //esb.ProviderConnectionString = @"Data Source=DP-201208111000;Initial Catalog=student;Integrated Security=True;MultipleActiveResultSets=True;"; //EntityConnection conn= new EntityConnection(esb.ConnectionString);//创建连接对象 //conn.Open(); #endregion #region EntityParameter //string con = "name = studentEntities "; //using (EntityConnection econn = new EntityConnection(con)) //{ // string esql = "select value c from studentEntities.T_StuInfo as c order by c.stuid skip @start limit @end"; // econn.Open(); // EntityCommand ecmd = new EntityCommand(esql, econn); // EntityParameter p1 = new EntityParameter("start", DbType.Int32);//构造参数 // p1.Value = 1; //为参数赋值 // EntityParameter p2 = new EntityParameter("end", DbType.Int32); // p2.Value = 4; // ecmd.Parameters.Add(p1);//将参数增加到命令对象中 // ecmd.Parameters.Add(p2); // //还可以使用下面的命令增加参数 // //ecmd.Parameters.AddWithValue("start", 0); // //ecmd.Parameters.AddWithValue("end", 4); // EntityDataReader ereader = ecmd.ExecuteReader(CommandBehavior.SequentialAccess); // while (ereader.Read()) // { // Console.WriteLine(ereader["name"]); // } // Console.WriteLine(ecmd.ToTraceString());//命令转化后使用了row_number()排序 //} #endregion #region EntityDataReader //string con = "name=studentEntities"; //using (EntityConnection econn = new EntityConnection(con)) //{ // string esql = "select value c from studentEntities.T_StuInfo as c order by c.stuid limit 10"; // econn.Open(); // EntityCommand ecmd = new EntityCommand(esql, econn); // EntityDataReader ereader = ecmd.ExecuteReader(CommandBehavior.SequentialAccess); // while (ereader.Read()) // { // Console.WriteLine("{0},{1},{2},{3}", ereader[0], ereader[1], ereader[2], ereader[3]); // } // Console.WriteLine(ecmd.ToTraceString()); // //注意:SequentialAccess 提供一种方法,以便 DataReader 处理包含带有大二进制值的列的行。 // //使用SequentialAccess则需按顺序访问列,否则将抛异常 //} #endregion Console.WriteLine("OK"); Console.Read(); } } }
注:本程序使用的ef源是:link to ef