public void QueryContactsEntityConecction() { using (var con = new EntityConnection("name=PEF")) { con.Open(); EntityCommand cmd = con.CreateCommand(); cmd.CommandText = "SELECT VALUE c " + "FROM PEF.Contact AS c " + "WHERE c.FirstName = 'Robert'"; Dictionary <int, string> dict = new Dictionary <int, string>(); using (EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection)) { while (rdr.Read()) { int a = rdr.GetInt32(0); var b = rdr.GetString(1); dict.Add(a, b); Console.WriteLine("{0} {1}", a, b); } } Console.Write("Press Enter..."); Console.ReadLine(); } #endregion }
/// <summary> /// ESQL /// </summary> static void RSQL() { // 这个就是App.config中的数据库连接串 using (var con = new EntityConnection("name=SchoolDBEntities")) { //打开连接 con.Open(); //通过连接创建一个命令对象 EntityCommand cmd = con.CreateCommand(); //设置要执行的SQL语句或存储过程 cmd.CommandText = "select value s from SchoolDBEntities.StudentSets as s"; //定义一个接收字典 Dictionary <int, string> dic = new Dictionary <int, string>(); //创建一个reader来进行数据读取 using (EntityDataReader rd = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess | System.Data.CommandBehavior.CloseConnection)) { while (rd.Read()) { int a = rd.GetInt32(0); string b = rd.GetString(1); dic.Add(a, b); } } } }
private static void VerifySortAscAndCountInt(EntityDataReader reader, int expectedCount) { var count = 0; int id = int.MinValue; while (reader.Read()) { var newId = reader.GetInt32(0); Assert.True(id <= newId); id = newId; count++; } Assert.Equal(count, expectedCount); }
//Querying with Object Services and Entity SQL //This doesn't work //static void QryOS() //{ // string sqlString = "SELECT VALUE st FROM SchoolDBEntities.Students " + // "AS st WHERE st.StudenName == 'Steve'"; // IObjectContextAdapter ctx = new // var objctx = (ctx as new IObjectContextAdapter).ObjectContext; // ObjectQuery<Student> student = objctx.CreateQuery<Student>(sqlString); // Student newStudent = student.First<Student>(); //} static void EntityConnection() { using (var con = new EntityConnection("name=SchoolDBEntities")) { con.Open(); EntityCommand cmd = con.CreateCommand(); cmd.CommandText = "SELECT VALUE st FROM SchoolDBEntities.Students as st where st.StudentName='Steve'"; Dictionary <int, string> dict = new Dictionary <int, string>(); using (EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection)) { while (rdr.Read()) { int a = rdr.GetInt32(0); var b = rdr.GetString(1); dict.Add(a, b); } } } }
/// <summary> /// EntityDataReader /// </summary> /// <returns></returns> public Dictionary <int, string> GetAllCityByEntityDataReader() { using (var con = new EntityConnection("name=ManagerSiContext")) { con.Open(); EntityCommand cmd = con.CreateCommand(); cmd.CommandText = "SELECT VALUE st FROM ManagerSiContext.BASE_CITY as st WHERE st.STATE == '1'"; Dictionary <int, string> dict = new Dictionary <int, string>(); using (EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection)) { while (rdr.Read()) { int a = rdr.GetInt32(0); var b = rdr.GetString(1); dict.Add(a, b); } } return(dict); } }
public static void EntitySQLUsingEntityConnectionDemo() { Console.WriteLine("*** EntitySQLUsingEntityConnectionDemo Start ***"); using (var con = new EntityConnection("name=SchoolDBEntities")) { con.Open(); EntityCommand cmd = con.CreateCommand(); cmd.CommandText = "SELECT VALUE st FROM SchoolDBEntities.Students as st where st.StudentID = 1"; Dictionary <int, string> dict = new Dictionary <int, string>(); using (EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection)) { while (rdr.Read()) { int a = rdr.GetInt32(0); var b = rdr.GetString(1); dict.Add(a, b); } } } Console.WriteLine("*** EntitySQLUsingEntityConnectionDemo Finished ***"); }