当前位置:数据库 > SQLServer >>

在.NET中根据SQL Server系统表获取数据库管理信息

对外提供访问接口的类代码如下(注意代码中DbUtility类是来自于《用C#打造自己的通用数据访问类库(续)》中的类,在周公处它们位于同一namespace下):
1. using System;
2. using System.Collections.Generic;
3. using System.Text;
4. using System.Data;
5. using System.Data.Sql;
6. using Microsoft.Win32;
7. 
8. namespace NetSkycn.Data
9. {
10.     /// <summary>
11.     /// 对外提供数据库管理信息的类
12.     ///
13.     ///
14.     /// www.zzzyk.com
15.     ///
16.     /// </summary>
17.     public class SqlServerManager
18.     {
19.         private static readonly string SQL_GetSysAltFiles = "use master;select * from SysAltFiles";
20.         private static readonly string SQL_GetSysColumns = "select * from SysColumns";
21.         private static readonly string SQL_GetSysDatabases = "use master;select * from SysDatabases";
22.         private static readonly string SQL_GetSysFiles = "select * from SysFiles";
23.         private static readonly string SQL_GetSysLogins = "use master;select * from SysLogins";
24.         private static readonly string SQL_GetSysObjects = "select * from SysObjects";
25.         private static readonly string SQL_GetSysTypes = "select * from SysTypes";
26.         private static readonly string SQL_GetSysUsers = "select * from SysUsers";
27. 
28.         private DbUtility dbUtility = null;
29.         public string ConnectionString { get; set; }
30.         public SqlServerManager()
31.         {
32. 
33.         }
34.         /// <summary>
35.         /// 创建SqlServerManager的实例
36.         /// </summary>
37.         /// <param name="connectionString"></param>
38.         public SqlServerManager(string connectionString)
39.         {
40.             this.ConnectionString = connectionString;
41.             dbUtility = new DbUtility(connectionString, DbProviderType.SqlServer);
42.         }
43.         /// <summary>
44.         /// 从主数据库中保存数据库的文件信息
45.         /// </summary>
46.         /// <returns></returns>
47.         public List<SysAltFile> GetSysAltFiles()
48.         {
49.             dbUtility.ConnectionString = ConnectionString;
50.             return dbUtility.QueryForList<SysAltFile>(SQL_GetSysAltFiles, null);
51.         }
52.         /// <summary>
53.         /// 从当前连接的数据库中获取所有列的信息
54.         /// </summary>
55.         /// <returns></returns>
56.         public List<SysColumn> GetSysColumns()
57.         {
58.             dbUtility.ConnectionString = ConnectionString;
59.             return dbUtility.QueryForList<SysColumn>(SQL_GetSysColumns, null);
60.         }
61.         /// <summary>
62.         /// 从主数据库中获取服务器中所有数据库的信息
63.         /// </summary>
64.         /// <returns></returns>
65.         public List<SysDatabase> GetSysDatabases()
66.         {
67.             dbUtility.ConnectionString = ConnectionString;
68.             return dbUtility.QueryForList<SysDatabase>(SQL_GetSysDatabases, null);
69.         }
70.         /// <summary>
71.         /// 获取当前连接的数据库的数据库物理文件信息
72.         /// </summary>
73.         /// <returns></returns>
74.         public List<SysFile> GetSysFiles()
75.         {
76.             dbUtility.ConnectionString = ConnectionString;
77.             return dbUtility.QueryForList<SysFile>(SQL_GetSysFiles, null);
78.         }
79.         /// <summary>
80.         /// 从主数据库中查询登陆帐号信息
81.         /// </summary>
82.         /// <returns></returns>
83.         public List<SysLogin> GetSysLogins()
84.         {
85.             dbUtility.ConnectionString = ConnectionString;
86.             return dbUtility.QueryForList<SysLogin>(SQL_GetSysLogins, null);
87.         }
88.         /// <summary>
89.         /// 获取当前连接的数据库中所有数据库对象
90.         /// </summary>
91.         /// <returns></r

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,