简体   繁体   English

从vb.net中的多个复选框将记录插入数据库

[英]Inserting records to database from multiple checkboxes in vb.net

I have a table like these: 我有一张这样的桌子:

Club Table contains: ClubID(IDENTITY), Name, Address 俱乐部表包含:ClubID(IDENTITY),名称,地址

Genre table contains: GenreID and Genre 流派表包含:GenreID和流派

ClubGenre table contains Club ID, GenreID ClubGenre表包含俱乐部ID,GenreID

Club has a one to many relationship with Genre 俱乐部与流派有一对多关系

I created an application that functions like a database editor. 我创建了一个功能类似于数据库编辑器的应用程序。 I have a group of checkbox contained in a group box labeled Genres. 我在标记为“类型”的组框中包含一组复选框。 It contains the Genre in the Genre table. 它在流派表中包含流派。 This is not databound though, I typed it manually. 虽然这不是数据绑定,但我手动键入了它。 I'm having troubles with my Add/Save button. 我的“添加/保存”按钮遇到了麻烦。 What I'm basically trying to do is this 我基本上想做的是

  • Click New record button 点击新建记录按钮
  • textboxes and checkboxes will be enabled 文本框和复选框将被启用
  • input data 输入数据
  • click save 点击保存
  • Insert Name and address into club table (I know how to do it up to this part) 在俱乐部表中插入姓名和地址(到目前为止,我知道该怎么做)

Since my ClubID is IDENTITY, it will be auto generated right. 由于我的ClubID为IDENTITY,因此它将自动生成。 I need this ClubID to insert data into my ClubGenre table. 我需要此ClubID才能将数据插入到我的ClubGenre表中。

Let's say checkbox 1 labeled Pop is checked . 假设标记为Pop的复选框1被选中。 In my genre table, it contains a record namely 1 as genreID and Pop as genre 在我的类型表中,它包含一条记录,即1作为genreID,Pop作为该类型

This means my insert statement will be like: 这意味着我的插入语句将类似于:

INSERT INTO Genre VALUES(*thenewlyaddedClubID*,1)

Now if checkbox 1,3,5 are checked it will be like: 现在,如果选中复选框1、3、5,它将像:

INSERT INTO Genre VALUES(*thenewlyaddedClubID*,1)
INSERT INTO Genre VALUES(*thenewlyaddedClubID*,3)
INSERT INTO Genre VALUES(*thenewlyaddedClubID*,5)

I've come up with a solution in retrieving the newly added CLUB ID but can't put it all together yet. 我已经提出了一个解决方案,用于检索新添加的CLUB ID,但目前还不能将它们放在一起。 I'm thinking of putting the new clubID in a hidden textbox using the statement 我正在考虑使用该语句将新的clubID放在隐藏的文本框中

SELECT MAX(clubID) AS barid FROM club

using the highest ID is error prone! 使用最高的ID容易出错!

You can retrieve the last inserted ID with the @@IDENTITY, maybe even better though would be SCOPE_IDENTITY, which returns the last ID within the scope. 您可以使用@@ IDENTITY检索最后插入的ID,即使是SCOPE_IDENTITY,它甚至可能更好,它可以返回范围内的最后一个ID。

here is an example from the MSDN: 这是来自MSDN的示例:

USE AdventureWorks2008R2;
GO
--Display the value of LocationID in the last row in the table.
SELECT MAX(LocationID) FROM Production.Location;
GO
INSERT INTO Production.Location (Name, CostRate, Availability, ModifiedDate)
VALUES ('Damaged Goods', 5, 2.5, GETDATE());
GO
SELECT @@IDENTITY AS 'Identity';
GO
--Display the value of LocationID of the newly inserted row.
SELECT MAX(LocationID) FROM Production.Location;
GO

edit: do you really want to use checkboxes for the genre? 编辑:您真的要使用该类型的复选框吗? Why not a combobox with genres and an add button, that puts clubIDs and GenreIDs into a separate helper table, so you can later add genres without changing the interface ... just sayin 为什么不选择一个带有流派和添加按钮的组合框,该组合框会将clubID和GenreID放入单独的帮助器表中,因此您以后可以在不更改界面的情况下添加流派...

Using the MAX(clubID) is not safe (we can't be sure how the database generates the ID's). 使用MAX(clubID)不安全(我们无法确定数据库如何生成ID)。 It is better to get the new ID based on the other values (Name and Address). 最好根据其他值(名称和地址)获取新的ID。

So you can do something like this: 因此,您可以执行以下操作:

Sub Add()
  '' // Do the insert to club table here

  '' // Get the new clubID:
  Dim sql As String = "SELECT clubID FROM Club WHERE Name='{0}' AND Address='{1}'"
  Dim clubId As Integer = ''RunTheSQl...(String.Format(sql, Name.Text , Address.Text)

  If Genre1.Checked Then
    sql = "INSERT INTO ClubGenre VALUES({0}, 1)"
    ''...RunTheSql...(String.Format(sql, clubId)
  End If  
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM