如何将checkedlistbox中的checked项插入SQL数据库?

时间:2017-04-22 04:14:41

标签: sql vb.net

我正在尝试将已检查的项目从checkedlistbox保存到我的SQL数据库,并且我从同一个SQL数据库中填充了checkedlistbox,到目前为止,我能够从checkedlistbox中获取已检查项目的文本,并将其保存在字符串然后我使用一个标签来显示我是否获得已检查项目的文本及其工作,但当我尝试在数据库中插入已检查的数据时,我收到错误“连接属性尚未初始化”。在ExecuteNonQuery()方法上。

Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim da As New SqlDataAdapter
        Dim dt As New DataTable
        Dim connectionString As String = "Server=DESKTOP-V12PTAV ;Database=test ;User Id=sa ;Password=wills8877"
        Using conn As New SqlConnection(connectionString)
            conn.ConnectionString = connectionString
            conn.Open()
            Dim str As String
            str = "Select sem1 From sem"
            da = New SqlDataAdapter(str, conn)
            dt = New DataTable
            da.Fill(dt)
            CheckedListBox1.DataSource = dt
            CheckedListBox1.DisplayMember = "sem1"
            conn.Close()
        End Using
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


        Dim str As String
        Dim cmd As New SqlCommand
        Dim sql As String
        Dim connectionString As String = "Server=DESKTOP-V12PTAV ;Database=test ;User Id=sa ;Password=wills8877"
        Using conn As New SqlConnection(connectionString)
            conn.Open()
            Dim itemChecked As Object
            For Each itemChecked In CheckedListBox1.CheckedItems
                str = itemChecked.item("sem1").ToString
                Label1.Text = str
                sql = "insert into pretab(pre) values('" + str + "')"
                cmd.ExecuteNonQuery()
            Next
            conn.Close()

        End Using

    End Sub
End Class

This error

3 个答案:

答案 0 :(得分:1)

问题可能源于您的查询语法。试试这个:

sql = "insert into pretab(pre) values(@str)"
cmd.Parameters.AddWithValue("@str", str)            
cmd.ExecuteNonQuery()

OOPS ..我刚刚意识到你忘了给你的命令分配连接。所以,请尝试添加以下声明:

cmd = New SqlCommand(sql, conn)

执行你的命令。所以最终的代码应该是这样的:

sql = "insert into pretab(pre) values(@str)"

cmd = New SqlCommand(sql, conn)

cmd.Parameters.AddWithValue("@str", str) 

cmd.ExecuteNonQuery()

答案 1 :(得分:0)

首先,我会在一个类中进行数据操作,例如(注意我专注于插入)。您需要将服务器和目录更改为SQL Server上的服务器和目录。

Imports System.Data.SqlClient
Public Class Operations
    Private Server As String = "KARENS-PC"
    Private Catalog As String = "CheckedListBoxDatabase"
    Private ConnectionString As String = ""
    Public Sub New()
        ConnectionString = $"Data Source={Server};Initial Catalog={Catalog};Integrated Security=True"
    End Sub
    Public Function Read() As DataTable
        ' read rows for checked listbox here
    End Function
    Public Sub Insert(ByVal sender As List(Of String))
        Using cn As SqlConnection = New SqlConnection With {.ConnectionString = ConnectionString}
            Using cmd As SqlCommand = New SqlCommand With {.Connection = cn, .CommandText = "insert into pretab(pre) values (@pre)"}
                cmd.Parameters.Add(New SqlParameter With {.ParameterName = "@pre", .SqlDbType = SqlDbType.NVarChar})
                cn.Open()
                For Each item In sender
                    cmd.Parameters("@pre").Value = item
                    cmd.ExecuteNonQuery()
                Next
            End Using
        End Using
    End Sub
End Class

表格代码

Public Class Form1
    Private ops As New Operations
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Result = CheckedListBox1.Items.OfType(Of String).Where(Function(item, index) CheckedListBox1.GetItemChecked(index)).ToList
        ops.Insert(Result)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckedListBox1.DataSource = ops.Read
        CheckedListBox1.DisplayMember = "sem1"
    End Sub
End Class

答案 2 :(得分:0)

您似乎没有提供与命令的连接。您的代码是按钮单击

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim str As String
    Dim cmd As New SqlCommand
    Dim sql As String
    Dim connectionString As String = "Server=DESKTOP-V12PTAV ;Database=test ;User Id=sa ;Password=wills8877"
    Using conn As New SqlConnection(connectionString)
        conn.Open()
        Dim itemChecked As Object
        For Each itemChecked In CheckedListBox1.CheckedItems
            str = itemChecked.item("sem1").ToString
            Label1.Text = str
            sql = "insert into pretab(pre) values('" + str + "')"
            cmd.ExecuteNonQuery()
        Next
        conn.Close()

    End Using

End Sub

你需要做的是在cmd.ExecuteNonQuery()之前你需要为它提供一个连接cmd.connection = connectionString

这将删除cmd.ExecuteNonQuery()错误。