我试图一次运行一个查询,如果不存在,它会检查用户名,然后插入该值
public int CreateUser(string UserName,string Pwd)
{
cmd = new SqlCommand("CreateUser", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter name = cmd.Parameters.Add("@UserName", SqlDbType.VarChar);
name.Value = UserName;
SqlParameter password = cmd.Parameters.Add("@Password", SqlDbType.VarChar);
password.Value = Pwd;
DataSet ds = new DataSet();
adpt.SelectCommand = cmd;
adpt.Fill(ds);
conn.Close();
return 0;
}
我的存储过程是,
ALTER PROCEDURE [dbo].[CreateUser]
-- Add the parameters for the stored procedure here
@UserName varchar(200) = NULL,
@Password varchar(200) = NULL
AS
IF NOT EXISTS(select * from[dbo].[User] where
[dbo].[User].User_Name=@UserName)
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
insert into [dbo].[User] (User_Name,User_Password,User_Type)
values (@UserName,@Password,'User')
END
希望你的建议
提前致谢