Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 我有一个组合框,我必须用数据库名称填充它。请帮助我_Sql_Vb.net_Combobox_Devexpress - Fatal编程技术网

Sql 我有一个组合框,我必须用数据库名称填充它。请帮助我

Sql 我有一个组合框,我必须用数据库名称填充它。请帮助我,sql,vb.net,combobox,devexpress,Sql,Vb.net,Combobox,Devexpress,我有3个文本框和一个组合框,我必须在文本框中提供服务器名称、用户名和密码,组合框应该显示数据库名称。请帮助我我们是否可以假设,当您用sql标记此问题时,您实际上是指数据库是sql server?如果是这样,则要获取附加到实例的数据库列表,请使用适当的连接字符串创建一个SqlConnection,但不为Initial Catalog创建值。然后可以在该连接上调用GetSchema,并指定“Databases”作为集合。这将返回一个包含数据库数据的DataTable。数据库的名称将在“数据库名称”列

我有3个文本框和一个组合框,我必须在文本框中提供服务器名称、用户名和密码,组合框应该显示数据库名称。请帮助我

我们是否可以假设,当您用
sql
标记此问题时,您实际上是指数据库是sql server?如果是这样,则要获取附加到实例的数据库列表,请使用适当的连接字符串创建一个
SqlConnection
,但不为
Initial Catalog
创建值。然后可以在该连接上调用
GetSchema
,并指定“Databases”作为集合。这将返回一个包含数据库数据的
DataTable
。数据库的名称将在“数据库名称”列中。下面是我在对话中使用的一些代码,用户在对话中输入了连接详细信息,就像您所做的那样:


帮助什么?问题是什么?您尝试过什么?cmd.CommandText=“DatabaseNames\u SP”cmd.CommandType=CommandType.StoredProcedure DatabaseNames=cmd.ExecuteReader()cmbFromDatabase.Properties.Items.Add(DatabaseNames)这是我尝试过的方法,但显示出错误
Public Class OptionsDialogue

    Private populateServerList As Boolean = True
    Private populateDatabaseList As Boolean = True


    Private Sub OptionsDialogue_Load(ByVal sender As Object, _
                                     ByVal e As EventArgs) Handles MyBase.Load
        Me.LoadConnectionString()
        'Me.LoadAppSettings()
    End Sub

    Private Sub serverCombo_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles serverCombo.DropDown
        If Me.populateServerList Then
            'Enumerate available SQL Server instances.
            Dim serverTable As DataTable = SqlDataSourceEnumerator.Instance.GetDataSources()
            Dim upperBound As Integer = serverTable.Rows.Count - 1
            Dim serverNames(upperBound) As String

            For index As Integer = 0 To upperBound
                If serverTable.Rows(index).IsNull("InstanceName") Then
                    serverNames(index) = CStr(serverTable.Rows(index)("ServerName"))
                Else
                    serverNames(index) = String.Format("{0}\{1}", _
                                                       serverTable.Rows(index)("ServerName"), _
                                                       serverTable.Rows(index)("InstanceName"))
                End If
            Next

            Dim currentServerName As String = Me.serverCombo.Text

            With Me.serverCombo
                .BeginUpdate()
                .Items.Clear()
                .Items.AddRange(serverNames)
                .SelectedItem = currentServerName
                .Text = currentServerName
                .EndUpdate()
            End With

            Me.populateServerList = False
        End If
    End Sub

    Private Sub sqlServerSecurityOption_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sqlServerSecurityOption.CheckedChanged
        Dim sqlServerSecurity As Boolean = Me.sqlServerSecurityOption.Checked

        Me.userLabel.Enabled = sqlServerSecurity
        Me.userText.Enabled = sqlServerSecurity
        Me.passwordLabel.Enabled = sqlServerSecurity
        Me.passwordText.Enabled = sqlServerSecurity
    End Sub

    Private Sub databaseCombo_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles databaseCombo.DropDown
        Using connection As New SqlConnection(Me.GetConnectionString(False))
            Try
                connection.Open()

                'Enumerate available databases.
                Me.databaseCombo.DataSource = connection.GetSchema("Databases")
            Catch
                MessageBox.Show("Unable to connect.", _
                                "Connection Error", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error)
            End Try
        End Using
    End Sub

    Private Sub testButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles testButton.Click
        If Me.TestConnection() Then
            MessageBox.Show("Connection successful.", _
                            "Connection Successful", _
                            MessageBoxButtons.OK, _
                            MessageBoxIcon.Information)
        Else
            MessageBox.Show("Connection failed.", _
                            "Connection Failed", _
                            MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click
        If Me.TestConnection() OrElse _
           MessageBox.Show("A connection could not be established using the specified settings." & _
                           Environment.NewLine & _
                           "Do you still want to save the connection settings?", _
                           "Connection Failed", _
                           MessageBoxButtons.OKCancel, _
                           MessageBoxIcon.Warning, _
                           MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then
            Dim configSaved As Boolean

            configSaved = Me.SaveConnectionString
            'configSaved = Me.SaveAppSettings

            If configSaved OrElse _
               MessageBox.Show("The connection settings could not be saved." & _
                               Environment.NewLine & _
                               "Do you still want to close the window?", _
                               "Save Failed", _
                               MessageBoxButtons.OKCancel, _
                               MessageBoxIcon.Error, _
                               MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then
                Me.DialogResult = Windows.Forms.DialogResult.OK
            End If
        End If
    End Sub


    Private Sub LoadConnectionString()
        'Load the current connection string.
        Dim builder As New SqlConnectionStringBuilder(My.Settings.PrimaryConnectionString)

        'Display individual connection string properties.
        Me.serverCombo.Text = builder.DataSource
        Me.integratedSecurityOption.Checked = builder.IntegratedSecurity
        Me.sqlServerSecurityOption.Checked = Not builder.IntegratedSecurity
        Me.userText.Text = builder.UserID
        Me.passwordText.Text = builder.Password
        Me.databaseCombo.Text = builder.InitialCatalog
    End Sub

    Private Sub LoadAppSettings()
        'Open the primary config file.
        Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

        Dim appSettings As KeyValueConfigurationCollection = config.AppSettings.Settings
        Dim osSecurity As Boolean

        Boolean.TryParse(appSettings("OSSecurity").Value, osSecurity)

        'Display individual connection string properties.
        Me.serverCombo.Text = appSettings("ServerName").Value
        Me.integratedSecurityOption.Checked = osSecurity
        Me.sqlServerSecurityOption.Checked = Not osSecurity
        Me.userText.Text = appSettings("UserName").Value
        Me.passwordText.Text = appSettings("Password").Value
        Me.databaseCombo.Text = appSettings("DatabaseName").Value
    End Sub

    Private Function GetConnectionString(ByVal includeDatabase As Boolean) As String
        Dim builder As New SqlConnectionStringBuilder()

        'Build a connection string from the user input.
        builder.DataSource = Me.serverCombo.Text
        builder.IntegratedSecurity = Me.integratedSecurityOption.Checked
        builder.UserID = Me.userText.Text
        builder.Password = Me.passwordText.Text

        If includeDatabase Then
            builder.InitialCatalog = Me.databaseCombo.Text
        End If

        Return builder.ConnectionString
    End Function

    Private Function TestConnection() As Boolean
        Dim result As Boolean = False

        Using connection As New SqlConnection(Me.GetConnectionString(True))
            Try
                connection.Open()
                result = True
            Catch
            End Try
        End Using

        Return result
    End Function

    Private Function SaveConnectionString() As Boolean
        Dim result As Boolean = False

        'Open the primary config file.
        Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

        For Each setting As ConnectionStringSettings In config.ConnectionStrings.ConnectionStrings
            'Find the desired connection string.
            If setting.Name.Contains("PrimaryConnectionString") Then
                'Update the connection string from the user input.
                setting.ConnectionString = Me.GetConnectionString(True)

                'Save the changes to the config file.
                config.Save(ConfigurationSaveMode.Modified)
                ConfigurationManager.RefreshSection("connectionStrings")

                'Force the connection string to reload the new value from the config file.
                My.Settings.Reload()

                result = True

                Exit For
            End If
        Next setting

        Return result
    End Function

    Private Function SaveAppSettings() As Boolean
        Dim result As Boolean = False

        'Open the primary config file.
        Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

        Dim appSettings As KeyValueConfigurationCollection = config.AppSettings.Settings

        appSettings("ServerName").Value = Me.serverCombo.Text
        appSettings("DatabaseName").Value = Me.databaseCombo.Text
        appSettings("OSSecurity").Value = Me.integratedSecurityOption.Checked.ToString()
        appSettings("UserName").Value = Me.userText.Text
        appSettings("Password").Value = Me.passwordText.Text

        'Save the changes to the config file.
        config.Save(ConfigurationSaveMode.Modified)
        ConfigurationManager.RefreshSection("appSettings")

        result = True

        Return result
    End Function

End Class