【问题标题】:VB | Loading SQL Query into ComboboxVB |将 SQL 查询加载到组合框中
【发布时间】:2013-06-06 20:21:33
【问题描述】:

我正在尝试用 SQL 结果填充组合框 我认为我的问题是处理数据表形式的数据。

    Dim sql As String
    Dim sqlquery As String
    Dim ConnectionString As String
    ConnectionString = "Data Source=(local);Initial Catalog=Control;Persist Security Info=True;User ID=user;Password=pass"
    sqlquery = "Select dbName from Databases"

    Using connection As SqlConnection = New SqlConnection(ConnectionString)
        connection.Open()
        Using conn As SqlCommand = New SqlCommand(sqlquery, conn)
            Dim rs As SqlDataReader = comm.ExecuteReader
            Dim dt As DataTable = New DataTable
            dt.Load(cmboxDatabaseName)
        End Using 'comm
    End Using 'conn

当我运行程序时,我只是盯着一个悲伤的空组合框。

【问题讨论】:

    标签: sql vb.net visual-studio-2010 combobox


    【解决方案1】:

    几乎正确,但您需要使用 DataReader 加载数据表。
    然后将 DataTable 分配给 Combo 的 DataSource

    Using connection As SqlConnection = New SqlConnection(ConnectionString)
        connection.Open()
        Using comm As SqlCommand = New SqlCommand(sqlquery, connection)
                Dim rs As SqlDataReader = comm.ExecuteReader
                Dim dt As DataTable = New DataTable
                dt.Load(rs)
                ' as an example set the ValueMember and DisplayMember'
                ' to two columns of the returned table'
                cmboxDatabaseName.ValueMember = "IDCustomer"
                cmboxDatabaseName.DisplayMember = "Name"
                cmboxDatabaseName.DataSource = dt
        End Using 'comm
    End Using 'conn
    

    您还可以将组合框 ValueMember 属性设置为您将用作将来处理的键的列的名称,并将 DisplayMember 属性设置为您要显示为文本以供您选择的列名称用户

    【讨论】:

    • 我将此代码移动为按钮单击事件驱动器。至少它现在爆炸了 LOL ConnectionString = "ConnectionString = Data ... 立即注意到这一点。更改为 ConnectionString = "Data Source=(local);Initial Catalog=Control;Persist Security Info=True;User ID=user ;Password=pass" 现在说 Connection *** Highlighted ConnectionString = "ConnectionString = Data Source=(local);Initial Catalog=Control;Persist Security Info=True;User ID=user;Password=pass" 是我的连接错误吗?
    • 希望没有人受伤 :-) 你能告诉我错误信息吗?
    • 哈哈,我们只是失去了一些支持人员,没什么大不了 建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)
    • 想想我也搞混了一些骗局和通讯
    • 您是否在开发 PC 上安装了 SqlExpress?如果是,则应使用DataSource=.\SQLEXPRESS 而不是(local)
    【解决方案2】:

    你也可以这样做

    Dim Con = New SqlConnection(_ConnectionString)
    Dim cmdAs New SqlCommand
    Dim dr As New SqlDataReader
    
        Try
            If Con.State = ConnectionState.Closed Then
                Con.Open()
    
                cmd.Connection = Con
                cmd.CommandText = "Select field1, field2 from table"
    
    
                dr = cmd.ExecuteReader()
    
                ' Fill a combo box with the datareader
                Do While dr.Read = True
                    ComboBoxName.Items.Add(dr.GetString(0))
                    ComboBoxName.Items.Add(dr.GetString(1))
                Loop
    
                Con.Close()
            End If
    
        Catch ex As Exception
            MsgBox(ex.Message)
    
        End Try
    

    希望它对你有用。

    【讨论】:

      猜你喜欢
      • 2015-05-12
      • 2016-02-04
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多