【发布时间】:2009-10-28 14:33:24
【问题描述】:
在 SQL Server 2005 中使用以下存储过程
IF OBJECT_ID('[dbo].[UserProfile]') IS NOT NULL
DROP PROCEDURE [dbo].[UserProfile]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[UserProfile]
(
@UserId VARCHAR(20)
)
AS
IF @UserId = 'userId'
BEGIN
SELECT @UserId = 'Yes'
END
ELSE
SELECT @UserId = 'No'
SELECT @UserId AS RESULT
RETURN RESULT
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
C#代码,需要获取存储过程的结果
public String UserProfile(string userId)
{
String result;
System.Data.Common.DbCommand cmd = this.mConn.CreateCommand();
try
{
cmd.CommandTimeout = this.mCmdTimeout;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "UserProfile";
DbParameter p = cmd.CreateParameter();
p.ParameterName = "@UserId";
p.DbType = DbType.String;
p.Value = userId;
cmd.Parameters.Add(p);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
//Need to assign the value of the stored procedure's return to result
result = Convert.ToString(cmd.Parameters["@UserId"].Value);
}
catch (Exception ex)
{
throw;
}
finally
{
cmd.Connection.Close();
}
return result;
}
如何在 C# 中返回存储过程的结果?
【问题讨论】:
标签: c# .net sql stored-procedures