所以我有两个表,Employee和Login:
CREATE TABLE [dbo].[Employee] (
[EmpID] INT IDENTITY (1, 1) NOT NULL,
[ManagerID] INT NULL,
[EmpName] VARCHAR (50) NOT NULL,
[EmpRank] VARCHAR (50) NOT NULL,
[EmpDateOfBirth] DATE NOT NULL,
[EmpAddress] VARCHAR (100) NOT NULL,
[DeptID] INT NOT NULL,
[EmpSalary] INT DEFAULT ((0)) NOT NULL,
[EmpGender] VARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([EmpID] ASC),
CONSTRAINT [FK_Employee_Department] FOREIGN KEY ([DeptID]) REFERENCES [dbo].[Department] ([DeptID])
和
CREATE TABLE [dbo].[Login] (
[Username] VARCHAR (50) NOT NULL,
[Password] VARCHAR (50) NOT NULL,
[EmpID] INT NOT NULL IDENTITY,
PRIMARY KEY CLUSTERED ([Username] ASC),
CONSTRAINT [FK_Login_Employee] FOREIGN KEY ([EmpID]) REFERENCES [dbo].[Employee] ([EmpID])
所以我有一个页面表单来创建一个新的Employee,它向Employee和Login表添加信息。使用Employee表可以正常工作,但是使用Login表,我在Visual Studio中得到一个异常,因为EmpID'不能为null',即使在Employee表中它也设置为Identity。即使未将其设置为Identity,也会发生异常。所以我想知道我能做什么,以便我可以添加一个新的Employee,同时为两个表中的新记录保持相同的EmpID。
这是添加新信息的C#代码:
SqlCommand sqlc = new SqlCommand("Insert into Employee(EmpName, EmpRank, EmpDateOfBirth, EmpGender, DeptID, EmpSalary, EmpAddress) values (@EmpName, @EmpRank, @EmpDateOfBirth, @EmpGender, @DeptID, @EmpSalary, @EmpAddress)", connect);
sqlc.Parameters.AddWithValue("@EmpName", TextBoxName.Text);
sqlc.Parameters.AddWithValue("@EmpRank", DropDownListRank.Text);
sqlc.Parameters.AddWithValue("@EmpDateOfBirth", TextBoxDateOfBirth.Text);
sqlc.Parameters.AddWithValue("@EmpGender", DropDownListGender.Text);
sqlc.Parameters.AddWithValue("@DeptID", DropDownListDepartment.Text);
sqlc.Parameters.AddWithValue("@EmpSalary", TextBoxSalary.Text);
sqlc.Parameters.AddWithValue("@EmpAddress", TextBoxAddress.Text);
SqlCommand sqlc2 = new SqlCommand("Insert into Login(Username, Password) values (@Username, @Password)", connect);
sqlc2.Parameters.AddWithValue("@Username", TextBoxUsername.Text);
sqlc2.Parameters.AddWithValue("@Password", TextBoxPassword.Text);
connect.Open();
sqlc.ExecuteNonQuery();
sqlc2.ExecuteNonQuery();
connect.Close();
非常感谢任何帮助。
答案 0 :(得分:3)
嗯,首先要做的是删除Login表EmpID中的IDENTITY 这将是其他Employee表的相同ID,因此您不希望数据库为Login表生成可能的不同ID。
其次,您需要从Employee表中检索数据库自动分配的最后一个ID 这可以实现在第一个查询附加SELECT SCOPE_IDENTITY()并检索分配给Employee标识列的值,调用ExecuteScalar()
// Notice the semicolon at the end of the first query to separate
// the second command text. The result of this second command is returned
// by ExecuteScalar
SqlCommand sqlc = new SqlCommand(@"Insert into Employee(EmpName, EmpRank, EmpDateOfBirth,
EmpGender, DeptID, EmpSalary, EmpAddress)
values (@EmpName, @EmpRank, @EmpDateOfBirth,
@EmpGender, @DeptID, @EmpSalary, @EmpAddress);
SELECT SCOPE_IDENTITY()", connect);
sqlc.Parameters.AddWithValue("@EmpName", TextBoxName.Text);
sqlc.Parameters.AddWithValue("@EmpRank", DropDownListRank.Text);
sqlc.Parameters.AddWithValue("@EmpDateOfBirth", TextBoxDateOfBirth.Text);
sqlc.Parameters.AddWithValue("@EmpGender", DropDownListGender.Text);
sqlc.Parameters.AddWithValue("@DeptID", DropDownListDepartment.Text);
sqlc.Parameters.AddWithValue("@EmpSalary", TextBoxSalary.Text);
sqlc.Parameters.AddWithValue("@EmpAddress", TextBoxAddress.Text);
connect.Open();
int empid = Convert.ToInt32(sqlc.ExecuteScalar());
// Now pass the empid value to the second table
// Remember to remove the IDENTITY flag from Login.EmpID otherwise
// you will get an error.
SqlCommand sqlc2 = new SqlCommand(@"Insert into Login(EmpID, Username, Password)
values (@empid, @Username, @Password)", connect);
sqlc2.Parameters.AddWithValue("@empid", empid);
sqlc2.Parameters.AddWithValue("@Username", TextBoxUsername.Text);
sqlc2.Parameters.AddWithValue("@Password", TextBoxPassword.Text);
sqlc2.ExecuteNonQuery();
connect.Close();