将SQL实例名称和计算机名称放在同一组合框中 [英] Putting SQL Instance name And computer name in same combobox

查看:147
本文介绍了将SQL实例名称和计算机名称放在同一组合框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的.我有以下代码.这将实例放在组合框中,但是我需要它来放置计算机名称和实例,以便我可以正确连接到数据库.

这就是我需要组合框中的所有实例都看起来像的样子.


计算机名\实例
计算机名\实例

这就是现在所做的

实例
实例

这可能是一个非常容易回答的问题,我无法一生解决.


Ok. I have the follow code. This puts the Instance in the combobox but i need it to put the computer name and instance so i can connect to the database properly.

This is what i need all the instances in the combo box to look like.


Computername\Instance
Computername\Instance

This is what it does now

Instance
Instance

This is probably a very easy question to answer i just cannot figure it out for the life of me.


compname = System.Net.Dns.GetHostName()

Dim regKey As Microsoft.Win32.RegistryKey = _
           Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL")
        Dim instances() As String = DirectCast(regKey.GetValueNames(), String())

        ComboServers.DataSource = instances

推荐答案

您正在将实例名称列表分配给DataSource-但尚未将其与计算机名称组合在一起.

您需要在instances()中迭代一组名称,并为每个名称添加(前缀)计算机名称和"\"字符.

对不起,我没有使用VB.Net,所以不能给你VB代码.在使用Linq的C#中,它看起来像这样:

You are assigning the list of instance names to the DataSource - but you have not combined it with the name of the computer.

You need to iterate the set of names in instances() and, for each one, add (prefix) the computer name and the ''\'' character.

I''m sorry I don''t use VB.Net,so can''t give you VB code; in C#, using Linq, it would look something like this:

ComboServers.DataSource = instances.Select(n => compname + "\\" + n).ToList()



希望有帮助,

克里斯



Hope that helps,

Chris


对于需要此功能的任何人都是VB.NET,这是上面的转换后答案.

For anyone that needs this is VB.NET this is the converted answer from above.

Dim regKey As Microsoft.Win32.RegistryKey = _
               Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL")
            Dim instances() As String = DirectCast(regKey.GetValueNames(), String())

            ComboServers.DataSource = instances.[Select](Function(n) compname & "\" & Convert.ToString(n)).ToList()

        Catch ex As Exception
            MsgBox("Could not get SQL instances. You will have to type in the instance manually.", MsgBoxStyle.Exclamation)
        End Try


我不是确定你有什么问题...

请查看以下内容:如何在网络中枚举SQL Server实例 [ ^ ]
I''m not sure what kind of issue do you have...

Please, see this: How to enum SQL Server instances in network[^]


这篇关于将SQL实例名称和计算机名称放在同一组合框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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