如何修复列表框插入数据库 [英] How to fix listbox insert into database

查看:87
本文介绍了如何修复列表框插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我Mange先解决我的问题,然后将列表框项目插入数据库,但只插入了列表框中的第1项。



我是什么尝试过:



这是我的代码:



I Mange to solve my problem earlier inserting a listbox items into database, but only the 1st item in the listbox is being inserted.

What I have tried:

This is my code:

Try
          connect()
          connection.Open()
          Dim queryString As String
          queryString = "Insert into ServiceRecords([Personnel]) Values(@Personnels)"
          command = New OleDbCommand(queryString, connection)
          For i As Integer = 0 To Me.ListBox1.Items.Count + 1
              command.Parameters.AddWithValue("@Personnels", ListBox1.Items(i))
          Next
      Catch ex As Exception

      End Try

推荐答案

你的循环都错了;您将每个列表框项添加到相同的参数名称。而且你永远不会执行任何命令,所以什么都不会进入数据库。你的循环需要依次获取每个ListBox项,将它添加到你正在做的参数集中,然后使用 ExecutNonQuery (如Maciej已告诉你的那样),发送项目到数据库。

Your loop is all wrong; you are adding every listbox item to the same parameter name. And you never execute any command so nothing will go to the database. Your loop needs to take each ListBox item in turn, add it to the parameter set as you are doing, and then use ExecutNonQuery (as Maciej already told you), to send the item to the database.
queryString = "Insert into ServiceRecords([Personnel]) Values(@Personnels)"
command = New OleDbCommand(queryString, connection)
For i As Integer = 0 To Me.ListBox1.Items.Count + 1
    command.Parameters.AddWithValue("@Personnels", ListBox1.Items(i))
    command.ExecuteNonQuery()
    command.Parameters.Clear()
Next



参见 OleDbCommand.ExecuteNonQuery Method(System.Data.OleDb) [ ^ ]。


这篇关于如何修复列表框插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆