读书人

关于VB2005枚举网络中SQL服务器的有关

发布时间: 2011-12-29 22:09:38 作者: rapoo

关于VB2005枚举网络中SQL服务器的问题
在下面的代码中可以枚举SQL服务器,
Dim instance As SqlDataSourceEnumerator = _
SqlDataSourceEnumerator.Instance
Dim table As System.Data.DataTable = instance.GetDataSources()
DataRowView.DataSource = table
如果我想使用ComboBox来代替DataRowView,那上面的代码如果修改呢?谢谢!

[解决办法]
Imports System.Text
Imports System.Security.Cryptography
Imports SQLDMO
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents ComboBox2 As System.Windows.Forms.ComboBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.ComboBox2 = New System.Windows.Forms.ComboBox
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(112, 160)
Me.Button1.Name = "Button1 "
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1 "
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(88, 96)
Me.TextBox1.Name = "TextBox1 "
Me.TextBox1.Size = New System.Drawing.Size(120, 21)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = " "
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(88, 128)
Me.TextBox2.Name = "TextBox2 "
Me.TextBox2.Size = New System.Drawing.Size(120, 21)
Me.TextBox2.TabIndex = 1
Me.TextBox2.Text = " "
'
'ComboBox1
'
Me.ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.ComboBox1.Location = New System.Drawing.Point(88, 32)
Me.ComboBox1.Name = "ComboBox1 "
Me.ComboBox1.Size = New System.Drawing.Size(121, 20)
Me.ComboBox1.TabIndex = 2
'
'ComboBox2
'
Me.ComboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList


Me.ComboBox2.Location = New System.Drawing.Point(88, 64)
Me.ComboBox2.Name = "ComboBox2 "
Me.ComboBox2.Size = New System.Drawing.Size(121, 20)
Me.ComboBox2.TabIndex = 3
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.ComboBox2)
Me.Controls.Add(Me.ComboBox1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox2)
Me.Name = "Form1 "
Me.Text = "Form1 "
Me.ResumeLayout(False)

End Sub

#End Region


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


End Sub


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'得到SQL服务器的列表
'必须安装SQL SERVER 2000 SP2 及以上版本

Dim I As Short
Dim sqlApp As New SQLDMO.Application
Dim ServerName As SQLDMO.NameList
ServerName = sqlApp.ListAvailableSQLServers
For I = 1 To ServerName.Count
ComboBox1.Items.Add(ServerName.Item(I))
Next
End Sub


Private Sub ComboBox2_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.GotFocus

'得到指定SQL服务器所有数据库的列表
Try
'Dim sqlApp As New SQLDMO.Application
Dim oServer As New SQLDMO.SQLServer
oServer.Connect(Me.ComboBox1.Text.Trim, Me.TextBox1.Text.Trim, Me.TextBox2.Text.Trim)
ComboBox2.Items.Clear()
Dim db As SQLDMO.Database
For Each db In oServer.Databases
Me.ComboBox2.Items.Add(db.Name)
Next
Catch ex As Exception

End Try

End Sub
End Class


'注:vs2003下通过
[解决办法]
看2005的MSDN,里面有两个详细的例子
[解决办法]
ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/WD_ADONET/html/ddf1c83c-9d40-45e6-b04d-9828c6cbbfdc.htm


MSDN2005有高人研究出来,代码发上来.
[解决办法]
本机上试过,每台机只列出默认的实例名,其它实例都没列出来,如何能才把所有实例都列出来呢?

InstanceName
服务器实例的名称。如果服务器作为默认实例运行,则为空白。

读书人网 >VB Dotnet

热点推荐