如何保存复选框列表中的数据以访问数据库? [英] How to save data from checkedlistbox to access database?
本文介绍了如何保存复选框列表中的数据以访问数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好.我在根据清单框上选定的项目将数据插入数据库时遇到麻烦.有两个,并且清单框上的数据也来自相同的数据库,但来自不同的表,它们将用作指向数据库的外键. 我需要保存它们的表.我需要它,以便当用户完成选择项目并单击按钮时,它将立即将其保存到数据库中.这是我用来从数据库中获取两个清单框的数据的代码:
Hello. I'm having trouble with inserting data to a database based on items checked on a checkedlistbox. There are two and the data on the checkedlistboxes are also from the same database but from different tables and they will serve as foreign keys to the table where I need to save them. I need it so that when the user finishes selecting the items and click the button, it would instantly save it to the database. Here is the code that I use to get the data from the database for both checkedlistboxes:
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\rose&mike\Desktop\DbSysDel3\salondbaccess.accdb"),
adapter As New OleDbDataAdapter("SELECT Service_Name, Service_Fee FROM [Service_Types]", connection)
Dim sql As String = "Select Lastname FROM Employees2"
Dim cmd As New OleDbCommand
cmd = New OleDb.OleDbCommand(sql, connection)
connection.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader
If dr.HasRows Then
While dr.Read
chcklstbx2.Items.Add(dr(0))
End While
End If
Dim table As New DataTable
adapter.Fill(table)
With chcklstbx1
.DataSource = table
.DisplayMember = "Service_Name"
.ValueMember = "Service_Fee"
End With
connection.Close()
End Using
End Sub
推荐答案
Kareninstructor :
A great example on how to do this has been given by Kareninstructor in the following thread:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/e7c1e8e2-dd63-4126-a7d4-732118ce195f/how-to-save-and-retrieve-values-from-checkedlistbox-to-database?forum=vbgeneral
这篇关于如何保存复选框列表中的数据以访问数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文