【发布时间】:2014-01-14 17:43:00
【问题描述】:
我有一个有效的存储过程,当从旧的 ADO.NET 调用时它可以工作。以下是它:
GO
ALTER procedure [dbo].[GetParentID]
@SSHIP_AppID as varchar(50),
@ParentID as varchar(150) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SELECT @ParentID = a.iBuild_GUID
FROM dbo.XRef_iBuild_SSHIP as a
WHERE a.SSHIP_appId = @SSHIP_AppID
AND a.SSHIP_appId <> ''
END
在 ADO.NET 中,它接受一个 appID 并简单地返回一个字符串 parentId:
private string GetParentId(string appId)
{
var connection = new SqlConnection();
string parentId = String.Empty;
try
{
connection.ConnectionString = "Data Source="blah...";
var command = new SqlCommand("GetParentId", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@SSHIP_AppID", appId));
SqlParameter parent = new SqlParameter("@ParentID", SqlDbType.VarChar, 255);
parent.Direction = ParameterDirection.Output;
command.Parameters.Add(parent);
connection.Open();
command.ExecuteNonQuery();
parentId = (command.Parameters["@ParentID"].Value).ToString();
}
catch (Exception ex)
{
Logger.LogError(appId, ex.ToString(), "Interface12 - Cannot get ParentId", null, "", 0, "Interface12");
}
finally
{
connection.Close();
}
return parentId;
}
以下是我执行存储过程的函数导入时 EF 生成的内容:
public virtual int GetParentID(string sSHIP_AppID, ObjectParameter parentID)
{
var sSHIP_AppIDParameter = sSHIP_AppID != null ?
new ObjectParameter("SSHIP_AppID", sSHIP_AppID) :
new ObjectParameter("SSHIP_AppID", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("GetParentID", sSHIP_AppIDParameter, parentID);
}
这是正确的吗?如果是这样,我可以有一些示例代码来获取 parentID 吗?如果没有,我怎样才能让它产生更好的东西?
【问题讨论】:
-
研究在 SqlConnection 和 SqlCommand 对象周围使用“using()”
标签: c# entity-framework stored-procedures