【问题标题】:Insert list box items to sql server将列表框项插入到 sql server
【发布时间】:2014-01-16 12:00:11
【问题描述】:

我正在尝试将列表框中的项目添加到 sql server 中的外键表中。

列表框中的所有项目都应添加到数据库中。

当前我卡在循环中,因为它没有收集列表框中的项目。

列表框中的项目不会被选中。

任何提示都会很棒!

Dim CommandText As String = "INSERT INTO code (id, Code, specialty) VALUES (@id, @ClientCode, @specialty);"
        Dim cmd As SqlCommand = New SqlCommand(CommandText, sqlconninsertDB)

For Each item As ListItem In lstspecialty.Items
                If Items.Count = lstspecialty.Items.Count Then
                    Try
                        With cmd.Parameters
                            .Add(New SqlParameter("@id", ID))
                            .Add(New SqlParameter("@ClientCode", Code))
                            .Add(New SqlParameter("@specialty", item.Value))
                        End With

                    cmd.ExecuteNonQuery()
                    Label.Text = "Data Inserted"
                Catch ex As Exception
                     ErrorLabel.Text = "Error Message"
                End Try
            End If
        Next

【问题讨论】:

  • cmd.CommandText的值是多少?
  • 请详细说明您的问题。一般来说,如果您遇到困难,您需要呼叫拖车。

标签: sql vb.net sql-server-2008-r2 listbox


【解决方案1】:
Dim SQL as String = "INSERT INTO YourTable(id, ClientCode, specialty) VALUES (@id, @ClientCode, @specialty)"

Dim cmd As New SqlClient.SqlCommand(SQL, Connection)

For Each item As ListItem In lstspecialty.Items
            Try
                    With cmd.Parameters
                        .Add(New SqlParameter("@id", item.ID))
                        .Add(New SqlParameter("@ClientCode", item.Code))
                        .Add(New SqlParameter("@specialty", item.Value))
                    End With
                    cmd.ExecuteNonQuery()

            Catch ex As Exception

                 'Whatever you want'

            End Try            
 Next

【讨论】:

    猜你喜欢
    • 2013-06-03
    • 1970-01-01
    • 2016-01-26
    • 2012-12-29
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    相关资源
    最近更新 更多