执行带输出参数的存储过程(原型)
时间:2010-11-21 来源:在路上-belm
CREATE PROCEDURE [dbo].[Login_Test]
(
@UserName nvarchar(50),
@flag int out
)
AS
if exists(SELECT * FROM Admin WHERE
UserName =@UserName)
begin
set @flag=11;
end
else
begin
set @flag=10;
end
GO
/// 带输出参数的存储过程原型
///
</summary>
/// <param name="sUserName"></param>
/// <returns></returns>
public int
testoutput(string sUserName)
{
SqlConnection
myConnection = new
SqlConnection(ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"].ToString());
SqlCommand scd = new SqlCommand();
scd.Connection = myConnection;
scd.CommandText = "Login_Test";
scd.CommandType = CommandType.StoredProcedure;
SqlParameter paramin =
scd.CreateParameter();
paramin.ParameterName = "@UserName";
paramin.DbType = DbType.String;
paramin.Size =
50;
paramin.Value = sUserName;
SqlParameter
paramout = scd.CreateParameter();
paramout.ParameterName =
"@flag";
paramout.DbType = DbType.Int32;
paramout.Size = 4;
paramout.Direction =
ParameterDirection.Output;
scd.Parameters.Add(paramin);
scd.Parameters.Add(paramout);
scd.ExecuteNonQuery();
myConnection.Close();
return (int)paramout.Value;
}
(
@UserName nvarchar(50),
@flag int out
)
AS
if exists(SELECT * FROM Admin WHERE
UserName =@UserName)
begin
set @flag=11;
end
else
begin
set @flag=10;
end
GO
执行方法
/// <summary>
/// 带输出参数的存储过程原型
///
</summary>
/// <param name="sUserName"></param>
/// <returns></returns>
public int
testoutput(string sUserName)
{
SqlConnection
myConnection = new
SqlConnection(ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"].ToString());
SqlCommand scd = new SqlCommand();
scd.Connection = myConnection;
scd.CommandText = "Login_Test";
scd.CommandType = CommandType.StoredProcedure;
//增加输入参数
SqlParameter paramin =
scd.CreateParameter();
paramin.ParameterName = "@UserName";
paramin.DbType = DbType.String;
paramin.Size =
50;
paramin.Value = sUserName;
//增加输出参数,多了个Direction少了个Value
SqlParameter
paramout = scd.CreateParameter();
paramout.ParameterName =
"@flag";
paramout.DbType = DbType.Int32;
paramout.Size = 4;
paramout.Direction =
ParameterDirection.Output;
//别忘了加入
scd.Parameters.Add(paramin);
scd.Parameters.Add(paramout);
myConnection.Open();
scd.ExecuteNonQuery();
myConnection.Close();
//返回
return (int)paramout.Value;
}
相关阅读 更多 +