存储的过程返回Varchar C#SQL Server
我需要输入一个变量,并
CREATE PROC CheckLogPas2
@log varchar(Max)
as
BEGIN
SET NOCOUNT ON;
DECLARE @k varchar(50);
set @k = (SELECT position From sotrud_users where login = @log);
RETURN @k;
END
在此过程中输出另一个变量,我希望将变量“ k”“位置”输入。 “位置”是varchar。当我尝试使用它的过程时,会出现错误。我无法输出变量“ k”,因为返回无法输出int int varible,
SqlCommand sqlcmd = new SqlCommand("[CheckLogPas]", conn);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.Parameters.AddWithValue("@log", SqlDbType.Int);
sqlcmd.Parameters["@UserID"].Value = DB_values.check_log;
SqlParameter param = new SqlParameter();
param.ParameterName = "@k";
param.SqlDbType = SqlDbType.NVarChar;
param.Direction = ParameterDirection.ReturnValue;
sqlcmd.ExecuteNonQuery();
comm.Parameters.Add(param);
conn.Open();
comm.ExecuteNonQuery();
pos = param.Value;
并且我的代码在c#中。我需要在C#上的另一个变量字符串中获取变量
I need input one variable and output another
CREATE PROC CheckLogPas2
@log varchar(Max)
as
BEGIN
SET NOCOUNT ON;
DECLARE @k varchar(50);
set @k = (SELECT position From sotrud_users where login = @log);
RETURN @k;
END
in this procedure i want take in variable "k" "position". "position" is varchar. When i try use it procedure give error. I can't output variable "k" because RETURN can't output not int variable
SqlCommand sqlcmd = new SqlCommand("[CheckLogPas]", conn);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.Parameters.AddWithValue("@log", SqlDbType.Int);
sqlcmd.Parameters["@UserID"].Value = DB_values.check_log;
SqlParameter param = new SqlParameter();
param.ParameterName = "@k";
param.SqlDbType = SqlDbType.NVarChar;
param.Direction = ParameterDirection.ReturnValue;
sqlcmd.ExecuteNonQuery();
comm.Parameters.Add(param);
conn.Open();
comm.ExecuteNonQuery();
pos = param.Value;
and this my code in c#. i need to get a variable in another variable string on c#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信a 标量值函数将解决您的问题,而不是存储过程。使用此功能,您将能够在SQL Server中创建一个接受参数的函数,并且可以返回值。如果您希望从函数中返回多个值,则需要使用表值函数。
以下是标量值函数的结构:
这是表值函数的结构:
您当前的存储过程:
可以将其重新编写为标量值,如
:您的C#代码执行以下查询:
喜欢:
I believe a Scalar-valued Function would solve your problem, instead of a Stored Procedure. Using this you would be able to create a function in SQL Server that accepts parameters, and can return a value. If you wish to return more than one value from your function, you would need to use a Table-valued Function instead.
Here is the structure for a Scalar-valued function:
And here is how a Table-valued Function would be structured:
Your current stored procedure:
Could be re-written as a Scalar-valued function like so:
Using that, you could instead have your c# code execute the following query:
Like so: