create proc [dbo].[userLauncher](@Phone varchar(13),@Passwork varchar(24)) --创建存储过程,参数@Phone,@Passwork
as
begin
declare @status int --声明游标
if exists(select * from[JXLing].[dbo].[UserInformation] where [userPhone]= @Phone and userPasswork = @Passwork) --判断用户名和密码是否存在
begin
insert into JXLing.dbo.UserLaunch ([userPhone],[launchTime]) values (@Phone,GETDATE())
if @@ERROR <> 0 --判断是否发生异常
set @status = 0; --赋值
else
set @status = 1;
end
else
begin
set @status = -1;
end
return @status;
end
ALTER proc [dbo].[userLauncher](@Phone varchar(13),@Passwork varchar(24)) --修改存储过程
as
begin
declare @status int --声明游标
if exists(select * from[JXLing].[dbo].[UserInformation] where [userPhone]= @Phone and userPasswork = @Passwork) --判断用户名和密码是否存在
begin
insert into JXLing.dbo.UserLaunch ([userPhone],[launchTime]) values (@Phone,GETDATE())
if @@ERROR <> 0 --判断是否发生异常
set @status = 0; --赋值
else
select top 1 @status= id from [ProductInformation] where id = @@IDENTITY
end
else
begin
select top 1 @status= id from [ProductInformation] where id = @@IDENTITY
end
return @status
end
declare @a int
select top 1 @a= id from [ProductInformation] where id = @@IDENTITY
go
exec userLauncher '1234567','1234567' --执行存储过程 return 需要游标接收,展示
C#调用存储过程
/// <summary>
/// 用户登陆存储过程 2017-03-27 17:28:21 linganyong
/// </summary>
/// <param name="userPhone"></param>
/// <param name="userPasswork"></param>
/// <returns></returns>
public SqlCommand userLauncher(UserInformation model) {
// localhost -- 链接地址 sa用户名
string connectionString = "Initial Catalog=JX900;Data Source=localhost;User ID=sa;Password=123456;Integrated Security=True";
SqlConnection sqlConnection = new SqlConnection(connectionString);
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlConnection.Open();
#region 用户登陆存储过程
SqlCommand com = new SqlCommand("userLauncher",sqlConnection); //存储过程名称
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@phone", model.UserPhone); //参数
com.Parameters.AddWithValue("@passwork", model.UserPasswork); //参数
SqlParameter parReturn = new SqlParameter("@status",SqlDbType.Int); //返回
parReturn.Direction = ParameterDirection.ReturnValue;
com.Parameters.Add(parReturn);
com.ExecuteNonQuery();
return com;
#endregion
}
获取存储过程的返回值 : int result = (int)cmd.Parameters["@status"].Value;