我从存储过程返回0和1,如下所示。我怎样才能在c#asp.net文件中获取它?
ALTER procedure [dbo].[crudprocedure]
@getusername varchar(20),
@getfirstname varchar(20),
@getlastname varchar(20),
@getaddress varchar(20)
as
begin
IF not exists (select username from crudtable where username=@getusername)
begin
insert into crudtable(username,firstname,lastname,address)
values (@getusername,@getfirstname,@getlastname,@getaddress)
return 0
end
else
return 1
end
答案 0 :(得分:1)
如果您使用的是SQL Server,请在此处尝试此代码:
using (SqlConnection conn = new SqlConnection("...put your connection string here..."))
using (SqlCommand cmd = new SqlCommand("dbo.crudprocedure", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@getusername", SqlDbType.VarChar, 20).Value = "test";
cmd.Parameters.Add("@getfirstname", SqlDbType.VarChar, 20).Value = "test";
cmd.Parameters.Add("@getlastname", SqlDbType.VarChar, 20).Value = "test";
cmd.Parameters.Add("@getaddress", SqlDbType.VarChar, 20).Value = "test";
// define additional paramter of Direction = ReturnValue
cmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
// open connection, execute stored proc, close connection
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
// fetch the value of the "ReturnValue" parameter
int returnValueInt = Convert.ToInt32(cmd.Parameters["RETURN_VALUE"].Value);
}
基本上,由于您使用存储过程中的RETURN 1
发送回该值,因此您需要向SqlCommand
.Direction
ParameterDirection.ReturnValue
对象添加一个额外参数1}}。
该参数将设置为使用RETURN ....