有必要收入Bolg:
1
int
i;
2
int
_count;
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
try
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
6
this.Cursor = Cursors.WaitCursor;
7
//Unmanaged SQLDMO call to retrieve the existing SQL Servers
8
object oNames;
9
Type SQLDMOApplication = Type.GetTypeFromProgID("SQLDMO.Application");
10
object oSQLApp = Activator.CreateInstance(SQLDMOApplication);
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
//If oSQLApp is null, then SQLDMO.DLL wasn't found installed to the system
13
if (oSQLApp == null)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
MessageBox.Show("SQL Data Model Objects assembly not found.");
16
return;
17
}
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
//Get Available SQL Servers
20
oNames = oSQLApp.GetType().InvokeMember("ListAvailableSQLServers", BindingFlags.IgnoreCase | BindingFlags.InvokeMethod, Type.DefaultBinder, oSQLApp, null, CultureInfo.CurrentCulture);
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
//Get SQL Server Count
23
_count = Convert.ToInt32(oNames.GetType().InvokeMember("Count", BindingFlags.IgnoreCase | BindingFlags.GetProperty, Type.DefaultBinder, oNames, null, CultureInfo.CurrentCulture), CultureInfo.CurrentCulture);
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
//Loop through the names and add them to the list
26
for (i = 1; i < _count; i++)
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
//Add Current SQL Server Name
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.listBox1.Items.Add(oNames.GetType().InvokeMember("Item", BindingFlags.IgnoreCase | BindingFlags.InvokeMethod, Type.DefaultBinder, oNames, new object[1]
{ i }, CultureInfo.CurrentCulture));
30
}
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
//Set the selection to the first item in the list
33
this.listBox1.SelectedIndex = 0;
34
}
35
catch
(System.InvalidCastException ex)
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
37
//SQLDMO object not found installed
38
MessageBox.Show(ex.Message);
39
}
40
catch
(System.ArgumentNullException ex2)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
42
//SQLDMO object not found installed
43
MessageBox.Show("Problem retrieving SQL Server List");
44
}
45
finally
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
47
this.Cursor = Cursors.Arrow;
48
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
其中需要用到2个Reference:
1
using
System.Reflection;
2
using
System.Globalization;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)