[LINQ] 如何调用 Stored Procedure From EntityFramework (2)

摘要:[LINQ] 如何调用 Stored Procedure From EntityFramework (2)


这次要说的是另一种读取方式,但是首先还是将Stored Procedure 加入Entity Model
如何加入请参考第一篇 : http://www.dotblogs.com.tw/constancy/archive/2013/10/31/126285.aspx

加入完之后,可用以下方式取得数据

  EntityConnection entityConnection = (EntityConnection) this.context.Connection;
            DbConnection storeConnection = entityConnection.StoreConnection;
            //建构参数
            SqlParameter parameter1= new SqlParameter("@parameter1", SqlDbType.Text);
            //为参数赋予值
            parameter1.Value = parameterValue;

            storeConnection.Open();
                using (DbCommand command = storeConnection.CreateCommand())
                {
                    
                    command.CommandText = "reg.getTapqSubinfo";
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.Add(parameter1);                  
                    //取得一列数据
                    var result = command.ExecuteReader(CommandBehavior.SingleRow);
                    if (result.Read())
                        //取得某一个字段的值
                        return result.GetString(3);
                }