ASP.NET 和 SQL - 将数据插入多个表中?

发布于 2024-10-07 18:54:14 字数 1495 浏览 3 评论 0原文

所以我知道以前也有人问过类似的问题,但我找不到适合我的具体情况的明确答案。我正在使用 ASP.NET(在 Visual Web Developer 中),并且需要将数据从一种表单插入到两个单独的表中。这是我的数据源:

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
    DataFile="~/App_Data/courseinfo.mdb" 
    SelectCommand="SELECT * FROM [tableCourse], [tableFaculty]"
    InsertCommand="INSERT INTO [tableCourse] 
    ([department], [name_first], [name_last], [prefix], 
    [course_number], [credits], [title], [description])
    VALUES (?, ?, ?, ?, ?, ?, ?, ?); INSERT INTO [tableFaculty] ([name_first], [name_last], [phone], [email])
    VALUES (?, ?, ?, ?)">

所以你看,我尝试使用两个插入语句,但它只是返回一个错误,指出 SQL 语句后面有多余的字符。我尝试去掉分号,然后它说我缺少一个分号。是否可以使用此控件一次插入两个表?如果没有,我该如何解决这个问题?

更新:

好的,在代码隐藏中尝试过,但我认为我做得不对,现在它给了我这个错误:

“/CCC”应用程序中的服务器错误。 索引或主键不能包含 Null 值。 描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.Data.OleDb.OleDbException:索引或主键不能包含 Null 值。

源错误:

Line 87: 
Line 88:         AccessDataSource1.InsertCommand = "INSERT INTO [tableCourse] ([department], [name_first], [name_last], [prefix], [course_number], [credits], [title], [description]) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
Line 89:         AccessDataSource1.Insert()
Line 90: 
Line 91:         AccessDataSource1.InsertCommand = "INSERT INTO [tableFaculty] ([name_first], [name_last], [phone], [email]) VALUES (?, ?, ?, ?)"

第 89 行是突出显示的那一行。所以我认为它正在尝试插入,但由于某种原因这些值为空,它没有从文本框中获取值。 我可能遗漏了一些明显的东西,我不知道,我对此真的很陌生。

So I know similar questions have been asked before, but I couldn't find a clear answer for my specific situation. I'm using ASP.NET (in Visual Web Developer) and I need to insert data from one form into two separate tables. This is my data source:

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
    DataFile="~/App_Data/courseinfo.mdb" 
    SelectCommand="SELECT * FROM [tableCourse], [tableFaculty]"
    InsertCommand="INSERT INTO [tableCourse] 
    ([department], [name_first], [name_last], [prefix], 
    [course_number], [credits], [title], [description])
    VALUES (?, ?, ?, ?, ?, ?, ?, ?); INSERT INTO [tableFaculty] ([name_first], [name_last], [phone], [email])
    VALUES (?, ?, ?, ?)">

So you see I've tried using two insert statements, and it just comes back with an error saying there are extra characters after the SQL statement. I've tried taking out the semi-colon and then it says I'm missing a semi-colon. Is it possible to insert to two tables at once using this control? And if not, how do I work around this?

UPDATE:

Okay, tried it in the codebehind, but I don't think I did it right, now it's giving me this error:

Server Error in '/CCC' Application.
Index or primary key cannot contain a Null value.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Index or primary key cannot contain a Null value.

Source Error:

Line 87: 
Line 88:         AccessDataSource1.InsertCommand = "INSERT INTO [tableCourse] ([department], [name_first], [name_last], [prefix], [course_number], [credits], [title], [description]) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
Line 89:         AccessDataSource1.Insert()
Line 90: 
Line 91:         AccessDataSource1.InsertCommand = "INSERT INTO [tableFaculty] ([name_first], [name_last], [phone], [email]) VALUES (?, ?, ?, ?)"

Line 89 is the one that's highlighted. So I'm thinking it's attempting to insert but for some reason the values are null, it isn't taking the values from the text boxes.
I probably left something obvious out, I don't know, I'm really new at this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心↖ 2024-10-14 18:54:14

为什么不在代码隐藏中尝试一下呢?

AccessDataSource1.InsertCommand = "First INSERT Statement";
AccessDataSource1.Insert();

AccessDataSource1.InsertCommand = "Second INSERT Statement";
AccessDataSource1.Insert();

Why don't you give it a try at the codebehind?

AccessDataSource1.InsertCommand = "First INSERT Statement";
AccessDataSource1.Insert();

AccessDataSource1.InsertCommand = "Second INSERT Statement";
AccessDataSource1.Insert();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文