引用Microsoft SQLDMO Object Library(SQLDMO.DLL)
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using Microsoft.SqlServer.Server;
5
6
namespace ConsoleApplication1
7
{
8
class Api
9
{
10
/// <summary>
11
/// 列出局域网中的所有数据库
12
/// </summary>
13
public void LoadAllSqlServer()
14
{
15
SQLDMO.ApplicationClass app = new SQLDMO.ApplicationClass();
16
SQLDMO.NameList nameList = app.ListAvailableSQLServers();
17
for (int i = 0; i < nameList.Count; i++)
18
{
19
if (nameList.Item(i).ToString().Length != 0)
20
{
21
Console.WriteLine(nameList.Item(i).ToString());
22
}
23
}
24
}
25
/// <summary>
26
/// 列出指定服务器的数据库列表
27
/// </summary>
28
/// <param name="serverName">服务器</param>
29
/// <param name="LoginName">登陆用户</param>
30
/// <param name="passWord">登陆密码</param>
31
public void LoadALlDatabase(string serverName, string LoginName, string passWord)
32
{
33
SQLDMO.SQLServerClass sqlServer = new SQLDMO.SQLServerClass();
34
try
35
{
36
sqlServer.Connect(serverName, LoginName, passWord);
37
for (int i = 0; i < sqlServer.Databases.Count; i++)
38
{
39
Console.WriteLine(sqlServer.Databases.Item(i).Name);
40
}
41
}
42
catch
43
{
44
Console.WriteLine("Error");
45
}
46
47
}
48
49
}
50
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50
