关于VB.NET多个combobox控件关联数据查询的问题

湘子的忧伤 2014-10-22 10:10:45



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Conn As New OleDb.OleDbConnection
Dim Comm As New OleDb.OleDbCommand
Dim da As New OleDb.OleDbDataAdapter
Dim ds As New DataSet

Conn.ConnectionString = "provider=sqloledb;Data Source=S1;Initial Catalog=yz_jc;Persist Security Info=True;User ID=sa;Password=sa"
Conn.Open()
'MsgBox("连接成功", vbOKOnly + vbInformation, "测试连接")
Comm.Connection = Conn
Comm.CommandText = "select * from jcfl where area_parent=0 order by area_order"
da = New OleDb.OleDbDataAdapter(Comm)
da.Fill(ds, "jcfl")
Conn.Close()
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.DisplayMember = "area_name"
ComboBox1.SelectedIndex = -1
End Sub

上面是部分代码,我只实现了第一个数据查询,后面的请问要怎么做

Comm1.CommandText = "select * from jcfl where area_parent=" & ComboBox1.Text & " order by area_order"
...全文
432 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
XIHONGSHI238 2014-10-27
  • 打赏
  • 举报
回复
学习了,楼主提的问题也是我想了解的,多谢!更谢谢回贴的各位!
wind_cloud2011 2014-10-23
  • 打赏
  • 举报
回复
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged Dim Comm As New SqlClient.SqlCommand Dim da As New SqlClient.SqlDataAdapter Dim ds As New DataSet Dim area_parent As Integer If ComboBox1.SelectedItem <> "" Then area_parent = Convert.ToInt16(ComboBox1.SelectedItem) sql = "select * from jcfl where area_parent=" & area_parent & "order by area_order" End If Dim Conn As New SqlClient.SqlConnection("Data Source=S1;Initial Catalog=yz_jc;User ID=sa;password=sa;Integrated Security=False") Conn.Open() Comm.Connection = Conn Comm.CommandText = sql da = New SqlClient.SqlDataAdapter(Comm) da.Fill(ds, "jcfl") DataGridView1.DataSource = ds.Tables(0).DefaultView Conn.Close() ComboBox2.DataSource = ds.Tables(0) ComboBox2.DisplayMember = "area_name" End Sub
湘子的忧伤 2014-10-23
  • 打赏
  • 举报
回复
引用 10 楼 sunny906 的回复:

    Dim Conn As New OleDb.OleDbConnection
    Dim da As New OleDb.OleDbDataAdapter
    Dim ds As New DataSet
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Conn.ConnectionString = "provider=sqloledb;Data Source=S1;Initial Catalog=yz_jc;Persist Security Info=True;User ID=sa;Password=sa"
        ds.Tables.Clear()
        ds.Clear()
        da = New OleDb.OleDbDataAdapter("select * from jcfl where area_parent=0 order by area_order", Conn)
        da.Fill(ds, "jcfl")
        ComboBox1.DataSource = ds.Tables(0)
        ComboBox1.DisplayMember = "area_name"
        ComboBox1.SelectedIndex = -1
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        ds.Tables.Clear()
        ds.Clear()
        da = New OleDb.OleDbDataAdapter("select * from jcfl where area_parent=" & ComboBox1.Text & " order by area_order", Conn)
        da.Fill(ds, "jcfl")
        ComboBox2.DataSource = ds.Tables(0)
        ComboBox2.DisplayMember = "area_name"
    End Sub
你好,这个combobox1不要判断是不是空吗?不然会说'order' 附近有语法错误,但是加了后,其他信息: 无法绑定由多个部分组成的标识符 "System.Data.DataRowView"
sunny906 2014-10-23
  • 打赏
  • 举报
回复

    Dim Conn As New OleDb.OleDbConnection
    Dim da As New OleDb.OleDbDataAdapter
    Dim ds As New DataSet
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Conn.ConnectionString = "provider=sqloledb;Data Source=S1;Initial Catalog=yz_jc;Persist Security Info=True;User ID=sa;Password=sa"
        ds.Tables.Clear()
        ds.Clear()
        da = New OleDb.OleDbDataAdapter("select * from jcfl where area_parent=0 order by area_order", Conn)
        da.Fill(ds, "jcfl")
        ComboBox1.DataSource = ds.Tables(0)
        ComboBox1.DisplayMember = "area_name"
        ComboBox1.SelectedIndex = -1
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        ds.Tables.Clear()
        ds.Clear()
        da = New OleDb.OleDbDataAdapter("select * from jcfl where area_parent=" & ComboBox1.Text & " order by area_order", Conn)
        da.Fill(ds, "jcfl")
        ComboBox2.DataSource = ds.Tables(0)
        ComboBox2.DisplayMember = "area_name"
    End Sub
湘子的忧伤 2014-10-23
  • 打赏
  • 举报
回复
引用 7 楼 sunny906 的回复:
1、在ComboBox1_SelectedIndexChanged事件里写Comm1.CommandText = "select * from jcfl where xxx=" & ComboBox1.Text & " order by area_order" 2、在ComboBox2_SelectedIndexChanged事件里写Comm2.CommandText = "select * from jcfl where xxx=" & ComboBox2.Text & " order by area_order"
帮我看看8楼的要怎么改,还有连接数据库,是每次都要连接一次吗
湘子的忧伤 2014-10-23
  • 打赏
  • 举报
回复


“System.Data.OleDb.OleDbException”类型的未经处理的异常出现在 System.Data.dll 中。

其他信息: 无法绑定由多个部分组成的标识符 "System.Data.DataRowView"。
我先就这样的 提示有错误,还有。我看的怪怪的,请问有没有好点的
拜一刀 2014-10-22
  • 打赏
  • 举报
回复
用一个sub来 Handles ComboBox1.SelectedIndexChanged,然后调用combobox1的值继续查询填充进combobox2,同理combobox3
sunny906 2014-10-22
  • 打赏
  • 举报
回复
1、在ComboBox1_SelectedIndexChanged事件里写Comm1.CommandText = "select * from jcfl where xxx=" & ComboBox1.Text & " order by area_order" 2、在ComboBox2_SelectedIndexChanged事件里写Comm2.CommandText = "select * from jcfl where xxx=" & ComboBox2.Text & " order by area_order"
wind_cloud2011 2014-10-22
  • 打赏
  • 举报
回复
多个ComboBox,都是查询条件,你都放在where中就可
wind_cloud2011 2014-10-22
  • 打赏
  • 举报
回复
从你连接语句看是sql: Dim Comm As New SqlClient.SqlCommand Dim da As New SqlClient.SqlDataAdapter Dim ds As New DataSet Dim Conn As New SqlClient.SqlConnection("Data Source=S1;Initial Catalog=yz_jc;User ID=sa;password=sa;Integrated Security=False") Conn.Open() Comm.Connection = Conn Comm.CommandText = "select top 10 * from where area_parent=0 order by area_order" da = New SqlClient.SqlDataAdapter(Comm) da.Fill(ds, "jcfl") DataGridView1.DataSource = ds.Tables(0).DefaultView Conn.Close() ComboBox1.DataSource = ds.Tables(0) ComboBox1.DisplayMember = "area_name" ComboBox1.SelectedIndex = -1
wind_cloud2011 2014-10-22
  • 打赏
  • 举报
回复
你是连接sql server ,还是连接mdb文件, 连接mdb的,例子 Dim cnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=信息.mdb" Dim sql As String = "select * from 信息 where 编号=" & Convert.ToInt16(TextBox1.Text) Dim cn As OleDb.OleDbConnection = New OleDb.OleDbConnection(cnStr) Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, cn) Dim ds As New DataSet da.Fill(ds, "信息") '将查询的数据填充到ds中
湘子的忧伤 2014-10-22
  • 打赏
  • 举报
回复
引用 2 楼 wind_cloud2011 的回复:
"select * from jcfl where area_parent='" & ComboBox1.Text & " ' order by area_order" 字符加单引号
那还需要在写连接数据库吗?能不能按照上面的给我改下代码
wind_cloud2011 2014-10-22
  • 打赏
  • 举报
回复
"select * from jcfl where area_parent='" & ComboBox1.Text & " ' order by area_order" 字符加单引号

16,556

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧