将CheckedListBox中的选中项添加到sql vb.net

时间:2017-05-14 18:56:36

标签: vb.net sqlite

我正在用公司名称填写CheckBoxList(这很好)

'Fill CheckBoxList'
Try
    con.Open()

    cmd.Connection = con
    cmd.CommandText = "SELECT CName FROM Company ORDER BY CName ASC"
    dr = cmd.ExecuteReader()
    Do While dr.Read = True
        CheckedListBox1.Items.Add(dr.GetString(0))
    Loop
Catch ex As Exception
    MsgBox(ex.Message)
Finally
    con.Close()
    cmd.Dispose()
End Try

现在我需要将已检查的名称插入到一个表中,每个公司名称都在其自己的Field

我在这里被困......

当我点击按钮显示记录时,我希望CheckBoxList选择字段中的公司名称(如果我选择Company2,Company4,Company8和Company10,CheckBoxList的Company1到Company10,当我拉出记录这些公司将被检查)

也许没有必要为每个公司单独的领域,我开放的想法......

由于

*编辑*

我在某个地方安装了它,但是它将CheckedItems插入到单独的行中

    Dim db As String = Datasource & filename & line & param
    Dim da As New SQLiteDataAdapter
    Dim dt As New DataTable
    Using con As New SQLiteConnection(db)
        con.Open()
        Dim checkedItem As Object
        For Each checkedItem In CheckedListBox1.CheckedItems
            cmd = con.CreateCommand
            cmd.CommandText = "INSERT INTO Mall (Company) VALUES ('" & checkedItem & "')"
            cmd.ExecuteNonQuery()
        Next
        con.Close()
    End Using

可以修改它来做我需要的吗?

1 个答案:

答案 0 :(得分:0)

以下将SQL-Server数据库表中的部门名称和ID加载到表单加载的CheckedListBox中。单击按钮会获取已检查的部门名称,并将它们传递给过程,以插入另一个表格或任何您想要执行的操作。

DisplayMember取自Department类中的ToString。

处理加载和处理数据的类

Imports System.Data.SqlClient
Public Class Operations
    ''' <summary>
    ''' Replace with your SQL Server name
    ''' </summary>
    Private Server As String = "KARENS-PC"
    ''' <summary>
    ''' Targeted database
    ''' </summary>
    Private Catalog As String = "ForumExamples"
    ''' <summary>
    ''' Connection string for connecting to the database
    ''' </summary>
    Private ConnectionString As String = ""
    ''' <summary>
    ''' Setup the connection string
    ''' </summary>
    Public Sub New()
        ConnectionString = $"Data Source={Server};Initial Catalog={Catalog};" &
            "Integrated Security=True"
    End Sub
    Public Function GetDepartments() As List(Of Department)
        Dim departments As New List(Of Department)

        Using cn As SqlConnection = New SqlConnection With {.ConnectionString = ConnectionString}
            Using cmd As SqlCommand = New SqlCommand With {.Connection = cn}
                cmd.CommandText = "SELECT id,Name FROM Departments"

                cn.Open()
                Dim reader As SqlDataReader = cmd.ExecuteReader
                While reader.Read
                    departments.Add(New Department With
                                    {
                                        .Id = reader.GetInt32(0),
                                        .Name = reader.GetString(1)
                                    })
                End While

            End Using
        End Using
        Return departments
    End Function
    Public Sub InsertDepartments(sender As List(Of Department))
        Using cn As SqlConnection = New SqlConnection With
            {
                .ConnectionString = ConnectionString
            }
            Using cmd As SqlCommand = New SqlCommand With
                {
                    .Connection = cn, .CommandText = "Your SQL INSERT statement"
                }
                cmd.Parameters.Add(New SqlParameter With
                                   {
                                    .ParameterName = "@Name", .DbType = DbType.String
                                   })
                cn.Open()
                For Each dept As Department In sender
                    cmd.Parameters("@Name").Value = dept.Name
                    cmd.ExecuteNonQuery()
                    Console.WriteLine(dept.Name)
                Next
            End Using
        End Using
    End Sub
End Class
Public Class Department
    Property Id As Integer
    Property Name As String
    Public Overrides Function ToString() As String
        Return Name
    End Function
End Class

表格代码

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If CheckedListBox1.CheckedItems.Count > 0 Then
        Dim departments As New List(Of Department)
        CheckedListBox1.CheckedItems.Cast(Of Department).ToList.ForEach(
            Sub(d)
                departments.Add(d)
            End Sub)
        ops.InsertDepartments(departments)
    Else
        MessageBox.Show("No departments selected")
    End If
End Sub