获取sql server服务器名称

获取sql server服务器名称以及一些相关设置
ContractedBlock.gif ExpandedBlockStart.gif 得到所有本地网络中可使用的SQL服务器列表 #region   得到所有本地网络中可使用的SQL服务器列表   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**////   <summary>   
InBlock.gif  
///   得到所有本地网络中可使用的SQL服务器列表   
InBlock.gif  
///   </summary>   
InBlock.gif  
///   <param   name="p_strServerList">服务器列表</param>   
ExpandedSubBlockEnd.gif  
///   <returns></returns>   

InBlock.gif  public   static   bool   GetServers(ref   string   []   p_strServerList)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
try   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  SQLDMO.Application   sqlApp   
=   new   SQLDMO.ApplicationClass();     
InBlock.gif  SQLDMO.NameList   sqlServers   
=   sqlApp.ListAvailableSQLServers();     
InBlock.gif  
if(sqlServers.Count   >   0)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  p_strServerList   
=   new   string[sqlServers.Count];   
InBlock.gif  
for(int   i=0;i<sqlServers.Count;i++)     
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{     
InBlock.gif  
string   srv   =   sqlServers.Item(i   +   1);     
InBlock.gif  
if(srv   !=   null)     
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{     
InBlock.gif  p_strServerList[i]   
=   srv;                                                     
ExpandedSubBlockEnd.gif  }
     
ExpandedSubBlockEnd.gif  }
     
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
return   true;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
catch(Exception   ex)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
throw   ex;   
ExpandedSubBlockEnd.gif  }
   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif    
ExpandedBlockEnd.gif  
#endregion
   
None.gif    
ContractedBlock.gifExpandedBlockStart.gif  
得到指定SQL服务器所有数据库的列表 #region   得到指定SQL服务器所有数据库的列表   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**////   <summary>   
InBlock.gif  
///   得到指定SQL服务器所有数据库的列表   
InBlock.gif  
///   </summary>   
InBlock.gif  
///   <param   name="p_strDataBaseList">数据库列表</param>   
InBlock.gif  
///   <param   name="p_strServer">服务器名</param>   
InBlock.gif  
///   <param   name="p_strUser">用户名</param>   
InBlock.gif  
///   <param   name="p_strPWD">密码</param>   
ExpandedSubBlockEnd.gif  
///   <returns></returns>   

InBlock.gif  public   static   bool   GetDataBases(ref   string   []   p_strDataBaseList,   string   p_strServer,   string   p_strUser,   string   p_strPWD)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
try   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
int   i   =   0;   
InBlock.gif    
InBlock.gif  SQLDMO.Application   sqlApp   
=   new   SQLDMO.ApplicationClass();     
InBlock.gif  SQLDMO.SQLServer   srv   
=   new   SQLDMO.SQLServerClass();                                     
InBlock.gif  srv.Connect(p_strServer,p_strUser,p_strPWD);     
InBlock.gif    
InBlock.gif  
if(srv.Databases.Count   >   0)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  p_strDataBaseList   
=   new   string[srv.Databases.Count];   
InBlock.gif    
InBlock.gif  
foreach(SQLDMO.Database   db   in   srv.Databases)     
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{     
InBlock.gif  
if(db.Name!=null)     
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  p_strDataBaseList[i]   
=   db.Name;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  i   
=   i   +   1;   
ExpandedSubBlockEnd.gif  }
   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
return   true;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
catch(Exception   ex)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
throw   ex;   
ExpandedSubBlockEnd.gif  }
   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif    
ExpandedBlockEnd.gif  
#endregion
   
None.gif    
ContractedBlock.gifExpandedBlockStart.gif  
得到所有的存储过程 #region   得到所有的存储过程   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**////   <summary>   
InBlock.gif  
///   得到所有的存储过程   
InBlock.gif  
///   </summary>   
InBlock.gif  
///   <param   name="p_strProcedureList">存储过程列表</param>   
InBlock.gif  
///   <param   name="p_strServer">服务器名</param>   
InBlock.gif  
///   <param   name="p_strUser">用户名</param>   
InBlock.gif  
///   <param   name="p_strPWD">密码</param>   
InBlock.gif  
///   <param   name="p_strDataBase">数据库名</param>   
ExpandedSubBlockEnd.gif  
///   <returns></returns>   

InBlock.gif  public   static   bool   GetProcedures(ref   string   []   p_strProcedureList,   string   p_strServer,   string   p_strUser,   string   p_strPWD,   string   p_strDataBase)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
try   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  SQLDMO.SQLServer   srv   
=   new   SQLDMO.SQLServerClass();                                     
InBlock.gif  srv.Connect(p_strServer,p_strUser,p_strPWD);     
InBlock.gif    
InBlock.gif  
for(int   i=0;i<srv.Databases.Count;i++)     
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{     
InBlock.gif  
if(srv.Databases.Item(i+1,"dbo").Name   ==   p_strDataBase)     
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{     
InBlock.gif  SQLDMO._Database   db
=   srv.Databases.Item(i+1,"dbo");     
InBlock.gif  
if   (db.StoredProcedures.Count   >   0)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  p_strProcedureList   
=   new   string[db.StoredProcedures.Count];   
InBlock.gif    
InBlock.gif  
for(int   j=0;j<db.StoredProcedures.Count;j++)     
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{     
InBlock.gif  p_strProcedureList[j]   
=   db.StoredProcedures.Item(j+1,"dbo").Name;     
ExpandedSubBlockEnd.gif  }
     
InBlock.gif  
break;     
ExpandedSubBlockEnd.gif  }
     
ExpandedSubBlockEnd.gif  }
   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif    
InBlock.gif  
return   true;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
catch(Exception   ex)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
throw   ex;   
ExpandedSubBlockEnd.gif  }
   
ExpandedSubBlockEnd.gif  }
   
ExpandedBlockEnd.gif  
#endregion
   
None.gif    
ContractedBlock.gifExpandedBlockStart.gif  
得到所有的Tables集合 #region   得到所有的Tables集合   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**////   <summary>   
InBlock.gif  
///   得到所有的Tables集合   
InBlock.gif  
///   </summary>   
InBlock.gif  
///   <param   name="p_strProcedureList">Tables集合</param>   
InBlock.gif  
///   <param   name="p_strServer">服务器名</param>   
InBlock.gif  
///   <param   name="p_strUser">用户名</param>   
InBlock.gif  
///   <param   name="p_strPWD">密码</param>   
InBlock.gif  
///   <param   name="p_strDataBase">数据库名</param>   
ExpandedSubBlockEnd.gif  
///   <returns></returns>   

InBlock.gif  public   static   bool   GetTables(ref   string   []   p_strTableList,   string   p_strServer,   string   p_strUser,   string   p_strPWD,   string   p_strDataBase)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
try   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  SQLDMO.SQLServer   srv   
=   new   SQLDMO.SQLServerClass();                                     
InBlock.gif  srv.Connect(p_strServer,p_strUser,p_strPWD);     
InBlock.gif    
InBlock.gif  
for(int   i=0;i<srv.Databases.Count;i++)     
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{     
InBlock.gif  
if(srv.Databases.Item(i+1,"dbo").Name   ==   p_strDataBase)     
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{     
InBlock.gif  SQLDMO._Database   db
=   srv.Databases.Item(i+1,"dbo");     
InBlock.gif  
if   (db.Tables.Count   >   0)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  p_strTableList   
=   new   string[db.Tables.Count];   
InBlock.gif    
InBlock.gif  
for(int   j=0;j<db.Tables.Count;j++)     
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{     
InBlock.gif  p_strTableList[j]   
=   db.Tables.Item(j+1,"dbo").Name;     
ExpandedSubBlockEnd.gif  }
     
InBlock.gif  
break;     
ExpandedSubBlockEnd.gif  }
     
ExpandedSubBlockEnd.gif  }
   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif    
InBlock.gif  
return   true;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
catch(Exception   ex)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
throw   ex;   
ExpandedSubBlockEnd.gif  }
   
ExpandedSubBlockEnd.gif  }
   
ExpandedBlockEnd.gif  
#endregion
   
None.gif    
ContractedBlock.gifExpandedBlockStart.gif  
得到所有的Views集合 #region   得到所有的Views集合   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**////   <summary>   
InBlock.gif  
///   得到所有的Views集合   
InBlock.gif  
///   </summary>   
InBlock.gif  
///   <param   name="p_strProcedureList">Views集合</param>   
InBlock.gif  
///   <param   name="p_strServer">服务器名</param>   
InBlock.gif  
///   <param   name="p_strUser">用户名</param>   
InBlock.gif  
///   <param   name="p_strPWD">密码</param>   
InBlock.gif  
///   <param   name="p_strDataBase">数据库名</param>   
ExpandedSubBlockEnd.gif  
///   <returns></returns>   

InBlock.gif  public   static   bool   GetViews(ref   string   []   p_strViewList,   string   p_strServer,   string   p_strUser,   string   p_strPWD,   string   p_strDataBase)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
try   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  SQLDMO.SQLServer   srv   
=   new   SQLDMO.SQLServerClass();                                     
InBlock.gif  srv.Connect(p_strServer,p_strUser,p_strPWD);     
InBlock.gif    
InBlock.gif  
for(int   i=0;i<srv.Databases.Count;i++)     
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{     
InBlock.gif  
if(srv.Databases.Item(i+1,"dbo").Name   ==   p_strDataBase)     
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{     
InBlock.gif  SQLDMO._Database   db
=   srv.Databases.Item(i+1,"dbo");     
InBlock.gif  
if   (db.Views.Count   >   0)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  p_strViewList   
=   new   string[db.Views.Count];   
InBlock.gif    
InBlock.gif  
for(int   j=0;j<db.Views.Count;j++)     
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{     
InBlock.gif  p_strViewList[j]   
=   db.Views.Item(j+1,"dbo").Name;     
ExpandedSubBlockEnd.gif  }
     
InBlock.gif  
break;     
ExpandedSubBlockEnd.gif  }
     
ExpandedSubBlockEnd.gif  }
   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif    
InBlock.gif  
return   true;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
catch(Exception   ex)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
throw   ex;   
ExpandedSubBlockEnd.gif  }
   
ExpandedSubBlockEnd.gif  }
   
ExpandedBlockEnd.gif  
#endregion
   
None.gif

转载于:https://www.cnblogs.com/oflying907/archive/2006/05/19/403996.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL Server服务器查询是指在一个SQL Server数据库中执行查询操作来访问另外一个SQL Server数据库中的数据。这种查询可以在不同的物理服务器上的不同数据库之间进行。 实现跨服务器查询主要依赖于SQL Server的分布式查询功能。下面是实现跨服务器查询的步骤: 1. 配置服务器链路:首先需要在源服务器和目标服务器之间创建一个服务器链路。这可以通过使用sp_addlinkedserver系统存储过程在源服务器上创建一个目标服务器的链接来实现。链路名称和目标服务器的地址都需要被指定。 2. 配置远程登录:为了能够在源服务器和目标服务器之间进行通信,需要在目标服务器上配置远程登录。这可以通过使用sp_addlinkedsrvlogin系统存储过程来实现。 3. 编写查询语句:在源服务器上编写查询语句,使用四段名来引用目标服务器上的表。四段名由服务器名称数据库名称、模式名称和表名称组成。 4. 执行查询:最后,在源服务器上执行查询语句。查询将会在源服务器上执行,并且通过服务器链路将查询结果传输到目标服务器上。 需要注意以下几点: - 执行跨服务器查询可能会影响性能,尤其是当涉及到大量数据传输时。 - 对于连接到其他数据库管理系统的SQL Server,可能需要使用其他函数和方法进行连接和查询。 - 需要在网络连接可靠性和安全性方面特别注意。 总之,SQL Server服务器查询允许我们在不同的SQL Server数据库之间获取数据,并且通过服务器链路实现数据传输和通信。这对于需要在多个数据库之间共享数据的业务场景非常有用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值