如何将值从列表框插入到SQL [英] How to insert Values from Listbox to SQL

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

问题描述

团队,



我有2个列表框。列表框1作为名称列表框2作为性别



我需要的是当我按下vb中的按钮时。应该将两个listbox的第一个值插入到sql中。一旦它插入这些值应该单独删除,直到两个列表框都是空的。



我有以下代码。我知道我做错了请纠正我。此代码创建重复和Nullvalue。



 对于 i = < span class =code-digit> 0    ListBox1.Items.Count  -   1  
对于 j = 0 ListBox2.Items.Count - 1
selected = ListBox1.Items(i).ToString
selected1 = ListBox2.Items(i).ToString
MsgBox(已选中)
con.ConnectionString = Provider = SQLOLEDB.1; Password = $ impadmins123; Persist Security Info = True;用户ID = simp;初始目录=员工信息;数据源= SRAVI6
con.Open()
cmd.Connection = con
cmd1.Connection = con
cmd.CommandT ext = 插入tic(名称)值('&选中& ')
cmd1.CommandText = 插入tic(新)值('& selected1& < span class =code-string>')
cmd.ExecuteNonQuery()
cmd1.ExecuteNonQuery()
con.Close()
下一步
下一步

解决方案

impadmins123; Persist Security Info = True; User ID = simp; Initial Catalog = Employee Information; Data Source = SRAVI6
con.Open()
cmd.Connection = con
cmd1.Connection = con
cmd.CommandText = 插入到tic(name)值('& selected& ')
cmd1.CommandText = 插入tic(新)值('&选定1& ')
cmd.ExecuteNonQuery()
cmd1.ExecuteNonQuery()
con.Close()
下一步
下一页


而不是询问一小部分,然后改变你在下一篇文章中所做的事情,发布你所做的事情,这样我们就可以做出更好的解决方案。



  Dim  myList  As  < span class =code-keyword>新字典(  字符串字符串
Dim 总计作为 整数 = ListBox1.Items.Count - 1
For 每个 i 作为 整数 = 0 总计
myList.Add(ListBox1.Items(i).ToString,ListBox2.Items(i)。 ToString)
下一步
对于 每个 kv As KeyValuePair( Of String 字符串 myList
' kv.Key =第一个值
' kv.Value =第二个值与第一个
' 匹配这些是您的插入语句
下一页


Team,

I have 2 listboxs. Listbox 1 as name listbox 2 as sex

what i need is when i press a button in vb. the first value of both listbox alone should be inserted to sql. once its inserted those value should alone remove and till both list box is empty.

I had this below code. i know am doing things wrong please correct me. this code is creating duplicate and Nullvalue.

For i = 0 To ListBox1.Items.Count - 1
            For j = 0 To ListBox2.Items.Count - 1
                selected = ListBox1.Items(i).ToString
                selected1 = ListBox2.Items(i).ToString
                MsgBox(selected)
                con.ConnectionString = "Provider=SQLOLEDB.1;Password=$impadmins123;Persist Security Info=True;User ID=simp;Initial Catalog=Employee Information;Data Source=SRAVI6"
                con.Open()
                cmd.Connection = con
                cmd1.Connection = con
                cmd.CommandText = "insert into tic (name) values ('" & selected & "')"
                cmd1.CommandText = "insert into tic (new) values ('" & selected1 & "')"
                cmd.ExecuteNonQuery()
                cmd1.ExecuteNonQuery()
                con.Close()
            Next
        Next

解决方案

impadmins123;Persist Security Info=True;User ID=simp;Initial Catalog=Employee Information;Data Source=SRAVI6" con.Open() cmd.Connection = con cmd1.Connection = con cmd.CommandText = "insert into tic (name) values ('" & selected & "')" cmd1.CommandText = "insert into tic (new) values ('" & selected1 & "')" cmd.ExecuteNonQuery() cmd1.ExecuteNonQuery() con.Close() Next Next


Instead of asking a small part and then changing what your doing the next post, post what you are up to so we can make better solutions.

Dim myList As New Dictionary(Of String, String)
Dim total As Integer = ListBox1.Items.Count - 1
For Each i As Integer = 0 To total
  myList.Add(ListBox1.Items(i).ToString, ListBox2.Items(i).ToString)
Next
For Each kv As KeyValuePair(Of String, String) In myList
  ' kv.Key = the first value
  ' kv.Value = the second value paired with the first
  ' match these to your insert statements
Next


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

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