在Asp.net中如何用SQLDMO来获取SQL Server中的对象信息

前面写过一篇《如何用SQLDMOASP.NET页面下实现数据库的备份与恢复》的随笔,有朋友希望能多介绍一些SQLDMO的用法。现在,我简单介绍一下在Asp.net中如何用SQLDMO来获取SQL Server中的对象信息。首先我们想到的是所在的局域网内有多少SQL Server服务器,知道了服务器,有了登录的用户名和密码,就可以知道在指定的服务器上有多少张表,视图,存储过程,以及每张表中包含的字段信息等。

1.获取服务器的列表:

获取服务器时,主要是利用了SQLDMOApplication对象,该对象主要有以下几个主要的属性和方法:

方法:ListAvailableSQLServers()

属性:Name

           SQLServers

           Properties

示例程序:

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
 2InBlock.gif        /// 获取局域网内数据库服务器列表
 3ExpandedBlockEnd.gif        /// </summary>

 4 None.gif          private   void  FormatServerList()
 5 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 6InBlock.gif            sqlServers = sqlApp.ListAvailableSQLServers(); 
 7InBlock.gif
 8InBlock.gif            if(sqlServers != null)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
10InBlock.gif                for(int i=0;i<sqlServers.Count;i++
11ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif
12InBlock.gif                    object srv = sqlServers.Item( i + 1); 
13InBlock.gif                    
14ExpandedSubBlockStart.gifContractedSubBlock.gif                    /**////添加到服务器的列表中去
15InBlock.gif                    this.wdrp_ServerList.Items.Add(srv.ToString());                         
16ExpandedSubBlockEnd.gif                }

17ExpandedSubBlockEnd.gif            }

18ExpandedBlockEnd.gif        }

2.获取指定服务器的数据库列表:

这时需要创建一个SQLDMOSQLServer对象,用它来建立连接,并获取数据库列表,该对象的方法和属性:

方法:Connect(object servername,object Login,object Password)

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
 2InBlock.gif        /// 连接服务器
 3InBlock.gif        /// </summary>
 4InBlock.gif        /// <param name="ServerName">服务器名</param>
 5InBlock.gif        /// <param name="Login">登录名</param>
 6ExpandedBlockEnd.gif        /// <param name="Password">密码</param>

 7 None.gif          public   void  Connect( object  ServerName, object  Login, object  Password)
 8 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 9InBlock.gif        
10ExpandedBlockEnd.gif        }

属性:Databases

           Name

           Login

           Password

           QueryTimeOut

在获取数据库列表时,利用它的Databases属性,示例代码如下:

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
 2InBlock.gif        /// 获取指定服务器的数据库的列表
 3ExpandedBlockEnd.gif        /// </summary>
 4 None.gif          private   void  FormatDatabaseList()
 5 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 6InBlock.gif            SQLDMO.tSQLServer sr = new SQLDMO.SQLServerClass();
 7InBlock.gif            sr.Connect(this.wdrp_ServerList.SelectedItem.ToString(),txtUserName,txtUserPwd);
 8InBlock.gif            
 9ExpandedSubBlockStart.gifContractedSubBlock.gif            /**////创建一个DataTable
10InBlock.gif            DataTable dt = new DataTable();
11InBlock.gif            dt.Columns.Add("Name");
12InBlock.gif            dt.Columns.Add("Owner");
13InBlock.gif            dt.Columns.Add("Size");
14InBlock.gif            dt.Columns.Add("CreatDate");
15InBlock.gif
16InBlock.gif            foreach(Database db in sr.Databases)
17ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
18InBlock.gif                if(db.Name != null)
19ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
20InBlock.gif                    DataRow dr = dt.NewRow();
21InBlock.gif
22ExpandedSubBlockStart.gifContractedSubBlock.gif                    /**////获取数据库的名称
23InBlock.gif                    dr["Name"= db.Name;
24InBlock.gif
25ExpandedSubBlockStart.gifContractedSubBlock.gif                    /**////获取数据库的所有者
26InBlock.gif                    dr["Owner"= db.Owner;
27InBlock.gif
28ExpandedSubBlockStart.gifContractedSubBlock.gif                    /**////获取数据库的大小
29InBlock.gif                    dr["Size"= db.pSize;
30InBlock.gif
31ExpandedSubBlockStart.gifContractedSubBlock.gif                    /**////获取数据库的创建日期
32InBlock.gif                    dr["CreatDate"= db.CreateDate;
33InBlock.gif
34InBlock.gif                    dt.Rows.Add(dr);
35ExpandedSubBlockEnd.gif                }

36ExpandedSubBlockEnd.gif            }

37InBlock.gif            
38ExpandedSubBlockStart.gifContractedSubBlock.gif            /**////绑定数据
39InBlock.gif            this.wgrd_Database.DataSource = dt;
40InBlock.gif            this.wgrd_Database.DataBind();
41ExpandedBlockEnd.gif        }

3.获取数据中的表的集合:

这时需要创建一个SQLDMODatabase对象,利用它的属性和方法来获取,主要的属性和方法:

方法:Item(object Index,object Owner)

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
 2InBlock.gif        /// 获取指定项
 3InBlock.gif        /// </summary>
 4InBlock.gif        /// <param name="Index">索引</param>
 5InBlock.gif        /// <param name="Owner">所有者</param>
 6ExpandedBlockEnd.gif        /// <returns></returns>

 7 None.gif          public  Database Item( object  Index, object  Owner)
 8 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 9InBlock.gif            
10ExpandedBlockEnd.gif        }

属性:Name

           Owner

           Size

           CreateDate

           Tables

           Views

           StoredProcedures

示例代码如下:

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
 2InBlock.gif        /// 获取指定服务器,指定数据库的表集合
 3ExpandedBlockEnd.gif        /// </summary>

 4 None.gif          private   void  FormatTableList()
 5 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 6InBlock.gif            SQLDMO.SQLServer sr = new SQLDMO.SQLServerClass();
 7InBlock.gif            sr.Connect(this.wdrp_ServerList.SelectedItem.ToString(),txtUserName,txtUserPwd);
 8InBlock.gif            
 9ExpandedSubBlockStart.gifContractedSubBlock.gif            /**////创建一个DataTable
10InBlock.gif            DataTable dt = new DataTable();
11InBlock.gif            dt.Columns.Add("dbName");
12InBlock.gif            dt.Columns.Add("Name");
13InBlock.gif            dt.Columns.Add("Owner");
14InBlock.gif            dt.Columns.Add("CreatDate");
15InBlock.gif            dt.Columns.Add("PrimaryKey");
16InBlock.gif
17InBlock.gif            for(int j=0;j<sr.Databases.Count;j++
18ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
19InBlock.gif                if(sr.Databases.Item(j+1,"dbo").Name == this.wdrpDatabase.SelectedItem.ToString()) 
20ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif
21InBlock.gif                    SQLDMO.Database db= sr.Databases.Item(j+1,"dbo"); 
22InBlock.gif
23InBlock.gif                    for(int i=0;i<db.Tables.Count;i++)
24ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
25InBlock.gif                        DataRow dr = dt.NewRow();
26InBlock.gif
27ExpandedSubBlockStart.gifContractedSubBlock.gif                        /**////表所属数据库
28InBlock.gif                        dr["dbName"= db.Name;
29InBlock.gif                    
30ExpandedSubBlockStart.gifContractedSubBlock.gif                        /**////获取表名
31InBlock.gif                        dr["Name"= db.Tables.Item(i+1,"dbo").Name;
32InBlock.gif
33ExpandedSubBlockStart.gifContractedSubBlock.gif                        /**////获取表的所有者
34InBlock.gif                        dr["Owner"= db.Tables.Item(i+1,"dbo").Owner;
35InBlock.gif
36ExpandedSubBlockStart.gifContractedSubBlock.gif                        /**////获取表的创建日期
37InBlock.gif                        dr["CreatDate"= db.Tables.Item(i+1,"dbo").CreateDate;
38InBlock.gif                        
39ExpandedSubBlockStart.gifContractedSubBlock.gif                        /**////获取表的主键
40InBlock.gif                        dr["PrimaryKey"= db.Tables.Item(i+1,"dbo").PrimaryKey;
41InBlock.gif
42InBlock.gif                        dt.Rows.Add(dr);
43ExpandedSubBlockEnd.gif                    }

44ExpandedSubBlockEnd.gif                }

45ExpandedSubBlockEnd.gif            }

46ExpandedBlockEnd.gif        }

4.获取表中的其他信息:

我们看到,在SQLDMO中获取信息时对象是一级一级嵌套使用的,利用各个对象的属性来获取信息。同理,我们在获取表的信息时,同样需要创建一个SQLDMOTable的对象,它主要的属性和方法:

方法:Item(object Index,object Owner)

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
 2InBlock.gif        /// 获取指定项
 3InBlock.gif        /// </summary>
 4InBlock.gif        /// <param name="Index">索引</param>
 5InBlock.gif        /// <param name="Owner">所有者</param>
 6ExpandedBlockEnd.gif        /// <returns></returns>
 7 None.gif          public  Table Item( object  Index, object  Owner)
 8 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 9InBlock.gif            
10ExpandedBlockEnd.gif        }

属性:Name

           Owner

           CreateDate

           PrimaryKey

           Keys

           Triggers

            Indexs

           Rows

           Columns

利用这些属性和方法,大家可以很轻松地去获取数据表,字段等信息,这里就不写示例程序了,同样有了这些信息,就可以去写自己的代码生成器了^_^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值