使用SQLDMO操纵MS-SQL数据库

SQLDMO(SQL Distributed Management Objects,SQL分布式管理对象)是MS开发的一组COM组件。

None.gif                  // 取得服务器列表
None.gif
         private  ArrayList GetServerList()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif            System.Collections.ArrayList alServers 
= new System.Collections.ArrayList();
InBlock.gif            SQLDMO.Application sqlApp 
= new SQLDMO.ApplicationClass();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SQLDMO.NameList serverList 
= sqlApp.ListAvailableSQLServers();
InBlock.gif                
for(int i = 1; i <= serverList.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    alServers.Add(serverList.Item(i));
ExpandedSubBlockEnd.gif                }
 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch(Exception e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
throw(new Exception("取数据库服务器列表出错:"+e.Message));
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                sqlApp.Quit();
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
return alServers;
ExpandedBlockEnd.gif        }

None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif                
// 获得服务器上的数据库列表
None.gif
         private   bool  GetDbList( string  strServerName, string  strUserName, string  strPwd) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
bool bolReturn = false;
InBlock.gif            ArrayList alDbs 
= new ArrayList();
InBlock.gif            SQLDMO.Application sqlApp 
= new SQLDMO.ApplicationClass();
InBlock.gif            SQLDMO.SQLServer svr 
= new SQLDMO.SQLServerClass();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                svr.Connect(strServerName, strUserName, strPwd);
InBlock.gif                
foreach(SQLDMO.Database db in svr.Databases)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(db.Name!=null)
InBlock.gif                        alDbs.Add(db.Name);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
this.cmbDb.DataSource = alDbs;
InBlock.gif                bolReturn 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw(new Exception("连接数据库出错:"+e.Message));
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                svr.DisConnect();
InBlock.gif                sqlApp.Quit();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return bolReturn;
ExpandedBlockEnd.gif        }
 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif                
// 数据库备份
None.gif
         private   void  DbBackUp( string  savepath,  string  database)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            SQLDMO.SQLServer svr 
= new SQLDMO.SQLServerClass();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                svr.Connect(
this.server, this.userid, this.password);
InBlock.gif                SQLDMO.Backup bak 
= new SQLDMO.BackupClass();
InBlock.gif
InBlock.gif                SQLDMO.BackupSink_PercentCompleteEventHandler pceh 
= new SQLDMO.BackupSink_PercentCompleteEventHandler(SetBarVisible);
InBlock.gif                bak.PercentComplete 
+= pceh;
InBlock.gif
InBlock.gif                bak.Action 
= SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
InBlock.gif
InBlock.gif                bak.Initialize 
= true;
InBlock.gif                bak.Files 
= savepath + @"\1.bak";
InBlock.gif                bak.Database 
= database;
InBlock.gif                bak.SQLBackup(svr);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(System.Exception xx)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw(new Exception("备份数据库失败" + xx.Message));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                svr.DisConnect();
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值