HY,
我为数据库创建了以下3个表:
CREATE TABLE [dbo].[Buyer] (
[Buyer_Id] INT IDENTITY (1, 1) NOT NULL,
[Last_Name] NVARCHAR (50) NOT NULL,
[First_Name] NVARCHAR (50) NOT NULL,
[Social_No] NVARCHAR (50) NOT NULL,
[Phone] NVARCHAR (50) NOT NULL,
[User_Id] INT NOT NULL,
PRIMARY KEY CLUSTERED ([Buyer_Id] ASC),
CONSTRAINT [FK_User_Id] FOREIGN KEY ([User_Id]) REFERENCES [dbo].[User] ([User_Id])
);
CREATE TABLE [dbo].[Type] (
[Type_Id] INT IDENTITY (1, 1) NOT NULL,
[Type_Name] NCHAR (10) NULL,
PRIMARY KEY CLUSTERED ([Type_Id] ASC)
);
CREATE TABLE [dbo].[User] (
[User_Id] INT IDENTITY (1, 1) NOT NULL,
[User_Name] NCHAR (10) NOT NULL,
[Pass] NVARCHAR (50) NOT NULL,
[Type_Id] INT NOT NULL,
PRIMARY KEY CLUSTERED ([User_Id] ASC),
CONSTRAINT [FK__Type_Id] FOREIGN KEY ([Type_Id]) REFERENCES [dbo].[Type] ([Type_Id])
);
我有以下存储过程
CREATE PROCEDURE [dbo].[InsertCustomer]
@Buyer_Id int output,
@Last_Name varchar(50),
@First_Name varchar(50),
@Social_No varchar(50),
@Phone varchar(50),
@User_Id int output,
@User_Name nchar(10),
@Pass varchar(50),
@Type_id int output,
@Type_Name nchar(10)
AS
BEGIN
SET NOCOUNT ON;
insert into Buyer(Last_Name,First_Name,Social_No,Phone)
values (@Last_Name,@First_Name,@Social_No,@Phone)
set @Buyer_Id = SCOPE_IDENTITY();
insert into [User](User_Name,Pass)
values(@User_Name,@Pass)
set @User_Id = SCOPE_IDENTITY();
insert into [Type](Type_Name)
values (@Type_Name)
set @Type_id = SCOPE_IDENTITY();
RETURN 0
END
和C#代码:
SqlCommand cmd = new SqlCommand("InsertCustomer", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramLastName = new SqlParameter("@Last_Name", customer.Last_Name);
SqlParameter paramFirstName = new SqlParameter("@First_Name", customer.First_Name);
SqlParameter paramSocialNo = new SqlParameter("@Social_No", customer.Social_No);
SqlParameter paramPhone = new SqlParameter("@Phone", customer.Phone_No);
SqlParameter paramUserName = new SqlParameter("User_name", user.User_Name);
SqlParameter paramPass = new SqlParameter("@Pass", user.Pass);
SqlParameter paramTypeName = new SqlParameter("@Type_Name", type.Type_Name);
SqlParameter paramBuyerId = new SqlParameter("@Buyer_Id", SqlDbType.Int);
paramBuyerId.Direction = ParameterDirection.Output;
SqlParameter paramUserId = new SqlParameter("@User_Id", SqlDbType.Int);
paramUserId.Direction = ParameterDirection.Output;
SqlParameter paramTypeId = new SqlParameter("@Type_Id", SqlDbType.Int);
paramTypeId.Direction = ParameterDirection.Output;
cmd.Parameters.Add(paramLastName);
cmd.Parameters.Add(paramFirstName);
cmd.Parameters.Add(paramSocialNo);
cmd.Parameters.Add(paramPhone);
cmd.Parameters.Add(paramUserName);
cmd.Parameters.Add(paramPass);
cmd.Parameters.Add(paramTypeName);
cmd.Parameters.Add(paramBuyerId);
cmd.Parameters.Add(paramUserId);
cmd.Parameters.Add(paramTypeId);
conn.Open();
cmd.ExecuteNonQuery();
customer.Buyer_Id = (int)paramBuyerId.Value;
type.Type_Id = (int)paramTypeId.Value;
user.User_Id = (int)paramUserId.Value;
但是在我将数据插入使用上述代码的表单后,我收到以下错误:
无法将值NULL插入列' User_Id' .... PLATFORM.MDF.dbo.Buyer&#39 ;;列不允许空值。 INSERT失败。
无法将值NULL插入列' Type_Id',表格... PLATFORM.MDF.dbo.User&#39 ;;列不允许空值。 INSERT失败。
请帮助我,
此致
答案 0 :(得分:1)
存储过程中存在错误,因为您没有为User_Id或Type_Id列指定值,但这些列不允许使用空值。
您应该颠倒插入的顺序,并在以下部分中使用生成的标识值,如下所示:
insert into [Type](Type_Name)
values (@Type_Name)
set @Type_id = SCOPE_IDENTITY();
insert into [User](User_Name,Pass,TypeId)
values(@User_Name,@Pass,@Type_id)
set @User_Id = SCOPE_IDENTITY();
insert into Buyer(Last_Name,First_Name,Social_No,Phone,User_Id)
values (@Last_Name,@First_Name,@Social_No,@Phone,@User_Id)
set @Buyer_Id = SCOPE_IDENTITY();
答案 1 :(得分:1)
您应该更改插入顺序。首先,您应该插入新类型
insert into [Type](Type_Name)
values (@Type_Name)
set @Type_id = SCOPE_IDENTITY();
之后你应该插入新用户(使用@type_id)
insert into [User](User_Name,Pass,Type_id)
values(@User_Name,@Pass,@Type_id)
set @User_Id = SCOPE_IDENTITY();
Filnaly,使用@User_Id插入买方。反过来使用@User_Id
insert into Buyer(Last_Name,First_Name,Social_No,Phone,User_id)
values (@Last_Name,@First_Name,@Social_No,@Phone,@User_Id)
set @Buyer_Id = SCOPE_IDENTITY();