有没有办法获得此c#命令的结果(使用Entity Framework 6):
var result = _model.Database.SqlQuery<???>("EXEC sp_helptext dbo.AProcedure", new object());
我试过替换???以这种方式通过字符串:
var result = _model.Database.SqlQuery<string>("EXEC sp_helptext dbo.AProcedure", new object());
但只能获取sql执行的文本:EXEC sp_helptext dbo.AProcedure
答案 0 :(得分:0)
好的,因为您正在尝试执行系统存储过程,这不能从Entity Framework轻松完成,因此请使用DbCommand。试试这个
var entityConnection = MyDbContext.Database.Connection.ConnectionString;
DbConnection conn = entityConnection;
ConnectionState initialState = conn.State;
if (initialState != ConnectionState.Open)
conn.Open();
using (DbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "sp_helptext";
cmd.Parameters.Add(new SqlParameter("@name", "dbo.AProcedure"));
cmd.CommandType = CommandType.StoredProcedure;
var result = cmd.ExecuteScalar();
}
答案 1 :(得分:0)
我已经用这种方法完成了程序内容。
private string GetProcedureBody(string procedureName)
{
var data = _model.Database.SqlQuery<string>("exec sp_helptext @name",
new SqlParameter("name", procedureName));
var procedureLines = data.ToList();
return string.Join("", procedureLines.ToArray());
}
感谢@James Dev的帮助和解答。