无法使用mysql将数据存储在ASP.NET中的数据库中

编程入门 行业动态 更新时间:2024-10-26 20:26:47
本文介绍了无法使用mysql将数据存储在ASP.NET中的数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有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中的数据库中

本文发布于:2023-10-23 23:10:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522275.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   数据存储   mysql   NET   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!