封装SQLDMO操作的类

None.gif封装SQLDMO操作的类,能完成常用的SQL Server 2000管理工作。
None.gif使用前请添加 
"Microsoft SQLDMO Object Library" COM 引用。
None.gif有部分代码借鉴网络资料,再次向原作者表示感谢。
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//* **********************************************
InBlock.gif * Rainsoft Development Library for Microsoft.NET
InBlock.gif * Author: Q.yuhen (qyuhen@hotmail.com)
ExpandedBlockEnd.gif ********************************************** 
*/
 
None.gif
using System;
None.gif
using System.Collections;
None.gif
using System.Runtime.InteropServices;
None.gif
using System.IO;
None.gif
using SQLDMO;
None.gif
None.gif
namespace Rainsoft.Data
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// SQLDMO辅助类
InBlock.gif  
/// </summary>
InBlock.gif  
/// <remarks>
InBlock.gif  
/// 使用前添加 "Microsoft SQLDMO Object Library" COM 引用。
ExpandedSubBlockEnd.gif  
/// </remarks>

InBlock.gif  public class SqlDmoHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif    
DatabaseInfo#region DatabaseInfo
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 数据库信息
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public struct DatabaseInfo
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
public string Name;
InBlock.gif      
public string Owner;
InBlock.gif      
public string PrimaryFilePath;
InBlock.gif      
public string CreateDate;
InBlock.gif      
public int Size;
InBlock.gif      
public float SpaceAvailable;
InBlock.gif      
public string PrimaryName;
InBlock.gif      
public string PrimaryFilename;
InBlock.gif      
public int PrimarySize;
InBlock.gif      
public int PrimaryMaxSize;
InBlock.gif      
public string LogName;
InBlock.gif      
public string LogFilename;
InBlock.gif      
public int LogSize;
InBlock.gif      
public int LogMaxSize;
InBlock.gif
InBlock.gif      
public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
string s = "Name:{0}\r\n" +
InBlock.gif          
"Owner:{1}\r\n" +
InBlock.gif          
"PrimaryFilePath:{2}\r\n" +
InBlock.gif          
"CreateDate:{3}\r\n" +
InBlock.gif          
"Size:{4}MB\r\n" +
InBlock.gif          
"SpaceAvailable:{5}MB\r\n" +
InBlock.gif          
"PrimaryName:{6}\r\n" +
InBlock.gif          
"PrimaryFilename:{7}\r\n" +
InBlock.gif          
"PrimarySize:{8}MB\r\n" +
InBlock.gif          
"PrimaryMaxSize:{9}MB\r\n" +
InBlock.gif          
"LogName:{10}\r\n" +
InBlock.gif          
"LogFilename:{11}\r\n" +
InBlock.gif          
"LogSize:{12}MB\r\n" +
InBlock.gif          
"LogMaxSize:{13}MB";
InBlock.gif
InBlock.gif        
return string.Format(s, Name, Owner, PrimaryFilePath, CreateDate, Size,
InBlock.gif          SpaceAvailable, PrimaryName, PrimaryFilename, PrimarySize,
InBlock.gif          PrimaryMaxSize, LogName, LogFilename, LogSize, LogMaxSize);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
InBlock.gif    
private SQLServer2 sqlServer;
InBlock.gif    
private string server;
InBlock.gif    
private string login;
InBlock.gif    
private string password;
InBlock.gif
InBlock.gif    
public SqlDmoHelper(string server, string login, string password)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
this.server = server;
InBlock.gif      
this.login = login;
InBlock.gif      
this.password = password;
InBlock.gif
InBlock.gif      sqlServer 
= new SQLServer2Class();
InBlock.gif      sqlServer.Connect(server, login, password);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void Close()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      sqlServer.Close();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
Property#region Property
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取主要版本号
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public string Version
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif
InBlock.gif        
return string.Format("{0}.{1}", sqlServer.VersionMajor, sqlServer.VersionMinor); 
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取详细版本信息
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public string VersionString
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return sqlServer.VersionString;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取服务器时间
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public string ServerTime
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return sqlServer.ServerTime;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取系统服务名称
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public string ServiceName
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return sqlServer.ServiceName;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取或设置系统服务是否自动启动
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public bool AutostartServer
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return sqlServer.Registry.AutostartServer;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
set
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        sqlServer.Registry.AutostartServer 
= value;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取字符集设置
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public string CharacterSet
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return sqlServer.Registry.CharacterSet;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取服务器物理内存大小(MB)
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public int PhysicalMemory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return sqlServer.Registry.PhysicalMemory;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取服务器处理器(CPU)数量
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public int NumberOfProcessors
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return sqlServer.Registry.NumberOfProcessors;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
Public Method#region Public Method
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取网络内所有可用的服务器
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <returns></returns>

InBlock.gif    public static string[] ListAvailableSQLServers()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      NameList servers 
= new ApplicationClass().ListAvailableSQLServers();
InBlock.gif      
if (servers.Count <= 0return new string[0];
InBlock.gif
InBlock.gif      ArrayList list 
= new ArrayList(servers.Count);
InBlock.gif      
foreach (object o in servers) list.Add(o);
InBlock.gif      
return (string[])list.ToArray(typeof(string));
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 断开数据库所有连接
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <param name="dbName"></param>

InBlock.gif    public void KillAllProcess(string dbName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      QueryResults qr 
= sqlServer.EnumProcesses(-1) ; 
InBlock.gif      
InBlock.gif      
// 获取SPID和DBNAME字段列序号
InBlock.gif
      int iColPIDNum = -1 ; 
InBlock.gif      
int iColDbName = -1 ; 
InBlock.gif      
for(int i = 1; i <= qr.Columns; i++
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif
InBlock.gif        
string strName = qr.get_ColumnName(i) ; 
InBlock.gif        
InBlock.gif        
if (strName.ToUpper().Trim() == "SPID"
InBlock.gif          iColPIDNum 
= i ; 
InBlock.gif        
else if (strName.ToUpper().Trim() == "DBNAME"
InBlock.gif          iColDbName 
= i ; 
InBlock.gif
InBlock.gif        
if (iColPIDNum != -1 && iColDbName != -1
InBlock.gif          
break ; 
ExpandedSubBlockEnd.gif      }
 
InBlock.gif      
InBlock.gif      
// 将指定数据库的连接全部断开
InBlock.gif
      for(int i = 1; i <= qr.Rows; i++
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif
InBlock.gif        
int lPID = qr.GetColumnLong(i,iColPIDNum);
InBlock.gif        
string strDBName = qr.GetColumnString(i, iColDbName); 
InBlock.gif
InBlock.gif        
if (string.Compare(strDBName, "test"true== 0
InBlock.gif          sqlServer.KillProcess(lPID); 
ExpandedSubBlockEnd.gif      }
 
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取数据库信息
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="dbName"></param>
ExpandedSubBlockEnd.gif    
/// <returns></returns>

InBlock.gif    public DatabaseInfo GetDatabaseInfo(string dbName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Database db 
= GetDatabase(dbName);
InBlock.gif      
if (db == nullthrow new Exception("Database not exists!");
InBlock.gif
InBlock.gif      DatabaseInfo info 
= new DatabaseInfo();
InBlock.gif
InBlock.gif      info.Name 
= db.Name;
InBlock.gif      info.Owner 
= db.Owner;
InBlock.gif      info.PrimaryFilePath 
= db.PrimaryFilePath;
InBlock.gif      info.CreateDate 
= db.CreateDate;
InBlock.gif      info.Size 
= db.Size;
InBlock.gif      info.SpaceAvailable 
= db.SpaceAvailableInMB;
InBlock.gif        
InBlock.gif      DBFile primary 
= db.FileGroups.Item("PRIMARY").DBFiles.Item(1);
InBlock.gif      info.PrimaryName 
= primary.Name;
InBlock.gif      info.PrimaryFilename 
= primary.PhysicalName.Trim();
InBlock.gif      info.PrimarySize 
= primary.Size;
InBlock.gif      info.PrimaryMaxSize 
= primary.MaximumSize;
InBlock.gif
InBlock.gif      _LogFile log 
= db.TransactionLog.LogFiles.Item(1);
InBlock.gif      info.LogName 
= log.Name;
InBlock.gif      info.LogFilename 
= log.PhysicalName.Trim();
InBlock.gif      info.LogSize 
= log.Size;
InBlock.gif      info.LogMaxSize 
= log.MaximumSize;
InBlock.gif
InBlock.gif      
return info;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 分离数据库
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="dbName"></param>
InBlock.gif    
/// <remarks>
InBlock.gif    
/// 分离前最好调用KillAllProcess关闭所有连接,否则分离可能失败。
ExpandedSubBlockEnd.gif    
/// </remarks>

InBlock.gif    public void DetachDB(string dbName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      sqlServer.DetachDB(dbName, 
true);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 附加数据库
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="dbName"></param>
InBlock.gif    
/// <param name="dbFile"></param>
InBlock.gif    
/// <example>
InBlock.gif    
/// <code>
InBlock.gif    
/// SqlDmoHelper dmo = new SqlDmoHelper("(local)", "sa", "sa");
InBlock.gif    
/// dmo.AttachDB("test", @"d:\temp\database\test_data.mdf");
InBlock.gif    
/// </code>
ExpandedSubBlockEnd.gif    
/// </example>

InBlock.gif    public void AttachDB(string dbName, string dbFile)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      sqlServer.AttachDB(dbName, dbFile);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 删除数据库(文件也将被删除)
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <param name="dbName"></param>

InBlock.gif    public void KillDB(string dbName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      sqlServer.KillDatabase(dbName);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 创建数据库
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="dbName">数据库名称</param>
InBlock.gif    
/// <param name="path">数据文件保存路径</param>
InBlock.gif    
/// <param name="primaryFilename">数据库文件名(不含路径)</param>
InBlock.gif    
/// <param name="logFilename">日志文件名(不含路径)</param>
InBlock.gif    
/// <example>
InBlock.gif    
/// <code>
InBlock.gif    
/// SqlDmoHelper dmo = new SqlDmoHelper("(local)", "sa", "sa");
InBlock.gif    
/// dmo.CreateDB("test1", @"d:\temp\database", "abc.mdf", "abc1.ldf");
InBlock.gif    
/// </code>
ExpandedSubBlockEnd.gif    
/// </example>

InBlock.gif    public void CreateDB(string dbName, string path, string primaryFilename, string logFilename)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
// 创建数据库文件
InBlock.gif
      DBFile dbFile = new DBFileClass();
InBlock.gif      dbFile.Name 
= dbName + "_Data";
InBlock.gif      dbFile.PhysicalName 
= Path.Combine(path, primaryFilename);
InBlock.gif      dbFile.PrimaryFile 
= true;
InBlock.gif      
//dbFile.Size = 2; // 设置初始化大小(MB)
InBlock.gif      
//dbFile.FileGrowthType = SQLDMO_GROWTH_TYPE.SQLDMOGrowth_MB; // 设置文件增长方式
InBlock.gif      
//dbFile.FileGrowth=1; // 设置增长幅度
InBlock.gif
InBlock.gif      
// 创建日志文件
InBlock.gif
      _LogFile logFile = new LogFileClass();
InBlock.gif      logFile.Name 
= dbName + "_Log";
InBlock.gif      logFile.PhysicalName 
= Path.Combine(path, logFilename);
InBlock.gif      
//logFile.Size = 3;
InBlock.gif      
//logFile.FileGrowthType=SQLDMO_GROWTH_TYPE.SQLDMOGrowth_MB;
InBlock.gif      
//logFile.FileGrowth=1;
InBlock.gif 
InBlock.gif      
// 创建数据库
InBlock.gif
      Database db = new DatabaseClass();
InBlock.gif      db.Name 
= dbName;
InBlock.gif      db.FileGroups.Item(
"PRIMARY").DBFiles.Add(dbFile);
InBlock.gif      db.TransactionLog.LogFiles.Add(logFile);
InBlock.gif
InBlock.gif      
// 建立数据库联接,并添加数据库到服务器
InBlock.gif
      sqlServer.Databases.Add(db);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 备份数据库
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="dbName"></param>
InBlock.gif    
/// <param name="bakFile"></param>
InBlock.gif    
/// <param name="bakSetName"></param>
InBlock.gif    
/// <param name="bakDescription"></param>
InBlock.gif    
/// <example>
InBlock.gif    
/// <code>
InBlock.gif    
/// SqlDmoHelper dmo = new SqlDmoHelper("(local)", "sa", "sa");
InBlock.gif    
/// dmo.BackupDB("test", @"d:\temp\database\test.bak", "手动备份1", "备份说明dot.gif");
InBlock.gif    
/// </code>
ExpandedSubBlockEnd.gif    
/// </example>

InBlock.gif    public void BackupDB(string dbName, string bakFile, string bakSetName, string bakDescription)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Backup oBackup 
= new BackupClass();
InBlock.gif      oBackup.Action 
= SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
InBlock.gif      oBackup.Database 
= dbName;
InBlock.gif      oBackup.Files 
= bakFile;
InBlock.gif      oBackup.BackupSetName 
= bakSetName;
InBlock.gif      oBackup.BackupSetDescription 
= bakDescription;
InBlock.gif      oBackup.Initialize 
= true;
InBlock.gif      oBackup.SQLBackup(sqlServer);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 恢复数据库
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="dbName"></param>
InBlock.gif    
/// <param name="bakFile"></param>
InBlock.gif    
/// <remarks>
InBlock.gif    
/// 恢复前最好调用KillAllProcess关闭所有连接,否则恢复可能失败。
InBlock.gif    
/// </remarks>
InBlock.gif    
/// <example>
InBlock.gif    
/// <code>
InBlock.gif    
/// SqlDmoHelper dmo = new SqlDmoHelper("(local)", "sa", "sa");
InBlock.gif    
/// dmo.RestoreDB("test", @"d:\temp\database\test.bak");
InBlock.gif    
/// </code>
ExpandedSubBlockEnd.gif    
/// </example>

InBlock.gif    public void RestoreDB(string dbName, string bakFile)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Restore oRestore 
= new RestoreClass();
InBlock.gif      oRestore.Action 
= SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
InBlock.gif      oRestore.Database 
= dbName;
InBlock.gif      oRestore.Files 
= bakFile;
InBlock.gif      oRestore.FileNumber 
= 1;
InBlock.gif      oRestore.ReplaceDatabase 
= true;
InBlock.gif      oRestore.SQLRestore(sqlServer);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 收缩数据库
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <param name="dbName"></param>

InBlock.gif    public void ShrinkDB(string dbName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Database db 
= GetDatabase(dbName);
InBlock.gif      
if (db == nullthrow new Exception("Database not exists!");
InBlock.gif
InBlock.gif      db.Shrink(
0, SQLDMO_SHRINK_TYPE.SQLDMOShrink_Default);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取所有的数据库名
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <returns></returns>

InBlock.gif    public string[] ListAllDatabase()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      ArrayList list 
= new ArrayList();
InBlock.gif      
foreach(Database d in sqlServer.Databases)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        list.Add(d.Name);
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
if (list.Count == 0)
InBlock.gif        
return new string[0];
InBlock.gif      
else
InBlock.gif        
return (string[])list.ToArray(typeof(string));
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取所有登录名
InBlock.gif    
/// </summary>
InBlock.gif    
/// <returns></returns>
InBlock.gif    
/// <remarks>
InBlock.gif    
/// 管理工具 "安全性->登录"
ExpandedSubBlockEnd.gif    
/// </remarks>

InBlock.gif    public string[] ListAllLogins()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      ArrayList list 
= new ArrayList();
InBlock.gif      
foreach(Login d in sqlServer.Logins)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        list.Add(d.Name);
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
if (list.Count == 0)
InBlock.gif        
return new string[0];
InBlock.gif      
else
InBlock.gif        
return (string[])list.ToArray(typeof(string));
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取全部数据表名称
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="dbName"></param>
ExpandedSubBlockEnd.gif    
/// <returns></returns>

InBlock.gif    public string[] ListAllTables(string dbName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Database db 
= GetDatabase(dbName);
InBlock.gif      
if (db == nullthrow new Exception("Database not exists!");
InBlock.gif
InBlock.gif      ArrayList list 
= new ArrayList();
InBlock.gif      
foreach(Table t in db.Tables)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        list.Add(t.Name);
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
if (list.Count == 0)
InBlock.gif        
return new string[0];
InBlock.gif      
else
InBlock.gif        
return (string[])list.ToArray(typeof(string));
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取全部存储过程名称
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="dbName"></param>
ExpandedSubBlockEnd.gif    
/// <returns></returns>

InBlock.gif    public string[] ListAllStoredProcedure(string dbName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Database db 
= GetDatabase(dbName);
InBlock.gif      
if (db == nullthrow new Exception("Database not exists!");
InBlock.gif
InBlock.gif      ArrayList list 
= new ArrayList();
InBlock.gif      
foreach(StoredProcedure sp in db.StoredProcedures)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        list.Add(sp.Name);
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
if (list.Count == 0)
InBlock.gif        
return new string[0];
InBlock.gif      
else
InBlock.gif        
return (string[])list.ToArray(typeof(string));
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取数据库对象
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="dbName"></param>
InBlock.gif    
/// <returns></returns>
InBlock.gif    
/// <remarks>
InBlock.gif    
/// 可以通过数据库对象获取数据库内表、存储过程、触发器、数据类型等信息。
InBlock.gif    
/// </remarks>
InBlock.gif    
/// <example>
InBlock.gif    
/// 显示数据库中所有表及其结构
InBlock.gif    
/// <code>
InBlock.gif    
/// SqlDmoHelper dmo = new SqlDmoHelper("(local)", "sa", "sa");
InBlock.gif    
/// SQLDMO.Database db = dmo.GetDatabase("test");
InBlock.gif    
/// foreach(SQLDMO.Table t in db.Tables)
InBlock.gif    
/// {
InBlock.gif    
///    Console.WriteLine("Table:{0}", t.Name);
InBlock.gif    
///    for (int i = 1; i &lt;= t.Columns.Count; i++) // SQLDMO所有索引序号从1开始
InBlock.gif    
///    {
InBlock.gif    
///      SQLDMO._Column col = t.Columns.Item(i);
InBlock.gif    
///      Console.WriteLine(" Column:{0} DataType:{1}", col.Name, col.Datatype);
InBlock.gif    
///    }
InBlock.gif    
/// 
InBlock.gif    
///    Console.WriteLine("---------------");
InBlock.gif    
/// }
InBlock.gif    
/// </code>
ExpandedSubBlockEnd.gif    
/// </example>

InBlock.gif    public Database GetDatabase(string dbName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
foreach(Database d in sqlServer.Databases)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
if (string.Compare(d.Name, dbName, true== 0)
InBlock.gif          
return d;
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
return null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/nbwzy/archive/2007/06/05/771497.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值