手动创建连接字符串 [英] Creating Connection String Manually

查看:78
本文介绍了手动创建连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过从ComboBoxes中选择来创建连接字符串.我想在第一个组合框中加载DataSource,并相应地在第二个组合框中加载关联的数据库名称.
我正在使用SMO来执行此操作.我设法使用DataSource或Say Servernames加载了第一个组合框,并且在此Selected Changed Event上想加载第二个Combo,但是在触发事件时出现错误:Failed to Connect to the Server.问题出在哪里.
我的代码如下:

I want to create the connection string by selecting from ComboBoxes. I want to load DataSource in the First Combobox and accordingly load the second combobox with associated DataBase Name.
I am using SMO to do this .I managed to load first combobox with DataSource or Say Servernames and on this Selected Changed Event want to load the second Combo but I am getting error: Failed to Connect to the Server when event is fired. Where''s The Problem.
My Code Follows :

Public Sub fetchDataSource()
        Dim dataTable = SmoApplication.EnumAvailableSqlServers(False)
        For i As Integer = 0 To dataTable.Rows.Count - 1
            cmbDataSource.Items.Add(dataTable.Rows(i).Item(0))

        Next
      
    End Sub
 Private Sub cmbDataSource_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDataSource.SelectedIndexChanged
        cmbDatabase.Items.Clear()

        Dim serverName As String = cmbDataSource.SelectedText.ToString()
        Dim server As Server = New Server(serverName)

        For Each database As Database In server.Databases
            cmbDatabase.Items.Add(database.Name)
        Next
End Sub

推荐答案

您需要查看实际创建的字符串:它与VS通过数据库中的服务器资源管理器为您提供的字符串进行比较的方式属性?

您的代码没有帮助:我们不知道这些值或您对它们的实际用途!
You need to look at the string you are actually creating: How does it compare with the string that VS presents you via the Server Explorer in the Database Properties?

You code isn''t helpful: we don''t know the values or what you are actually doing with them!


您好,

尝试使用此代码来获取本地网络中的所有SQL Server实例.

Hi,

try this code to get all the SQL Server instances in the local Network.

System.Data.DataTable Dt = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();


Server obj = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection("<instancename>"));
</instancename>



问候
AR



Regards
AR


这篇关于手动创建连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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