有3个字段Id,name和department,Id是主键。在另一个表中,名称是EmployeeSalary,并且还有3个字段Id,EmpId和salary,EmpId应该是外键。每当我运行该程序时,我收到错误SQLEXCEPTION未被用户处理。断点显示在SQLCOMMAND对象上。这是插入查询。 我尝试过: String query =insert into dbo.Employee(Name,department)values(@ Name,@ Department); SqlCommand cmd = new SqlCommand(query,con); cmd.Parameters.AddWithValue(@ Name,txtname.Text); cmd.Parameters.AddWithValue(@ Department,DropDownList1.Text); cmd.CommandText = query; cmd.Connection = con; cmd.ExecuteNonQuery(); con.Close(); con.Open(); String secondQuery =insert into dbo.EmployeeSalary(Salary)values(@Salary); SqlCommand secondCmd = new SqlCommand(secondQuery,con); secondCmd.Parameters.AddWithValue(@ Salary,txtsalary.Text); secondCmd。 CommandText = secondQuery; secondCmd。 Connection = con; secondCmd.ExecuteNonQuery(); con.Close();
in one Employee table have 3 fields Id,name and department and Id is primary key. And in another table name is a EmployeeSalary and that has a also 3 fields Id,EmpId and salary and EmpId should be foreign key. whenever i run the program at that time i am getting error "SQLEXCEPTION unhandled by user". And breakpoint is show on the SQLCOMMAND object. And this is the insert query. What I have tried: String query = "insert into dbo.Employee(Name, department) values(@Name, @Department)"; SqlCommand cmd = new SqlCommand(query,con); cmd.Parameters.AddWithValue("@Name", txtname.Text); cmd.Parameters.AddWithValue("@Department", DropDownList1.Text); cmd.CommandText = query; cmd.Connection = con; cmd.ExecuteNonQuery(); con.Close(); con.Open(); String secondQuery = "insert into dbo.EmployeeSalary(Salary) values(@Salary)"; SqlCommand secondCmd = new SqlCommand(secondQuery,con); secondCmd.Parameters.AddWithValue("@Salary", txtsalary.Text); secondCmd.CommandText = secondQuery; secondCmd.Connection = con; secondCmd.ExecuteNonQuery(); con.Close();
推荐答案那里细节不够。我猜第一个表中的Id是Employee Id,第二个表在Insert期间期望Employee Id(empId)。但代码只提供薪水。第一个查询应该使用ExecuteScalar方法插入并返回Id,第二个查询应该使用Id作为EmpId。 以下是如何获取最后一个的示例代码插入ID。 返回上一个ID(IDENTITY)在插入行上VB.NET MySQL - 堆栈溢出 [ ^ ] There are not enough details. I'm guessing Id from the first table is the Employee Id, the second table is expecting Employee Id (empId) during the Insert. But the code only provide salary. The first query should Insert and return the Id using ExecuteScalar method and the second query should use the Id as EmpId. Here is a sample code on how to get the last insert Id. Return Last ID (IDENTITY) On Insert row VB.NET MySQL - Stack Overflow[^]
更多推荐
无法使用mysql将数据存储在ASP.NET中的数据库中
发布评论