日期:2014-05-18 浏览次数:20570 次
获取局域网内所有SqlServer
*-----------------------------------------------
* 函数: 获取局域网内所有SqlServer
* 设计: 红雨
* 时间: 2005.04.01
*-----------------------------------------------
Function NetEnumSqlServer( tcTableName )
m.tcTableName = Iif(Type([m.tcTableName])=[C], m.tcTableName, [TNetEnumSqlServer])
Create Cursor (m.tcTableName) ( ServerName C(254) )
Declare SHORT SQLBrowseConnect In odbc32 Integer ConnectionHandle, String InConnectionString, Integer StringLength1, String @ OutConnectionString, Integer BufferLength, Integer @ StringLength2Ptr
Declare SHORT SQLAllocHandle In odbc32 Integer HandleType, Integer InputHandle, Integer @ OutputHandlePtr
Declare SHORT SQLFreeHandle In odbc32 Integer HandleType, Integer Handle
Declare SHORT SQLSetEnvAttr In odbc32 Integer EnvironmentHandle, Integer Attribute, Integer ValuePtr, Integer StringLength
Local hEnv, hConn, cInString, cOutString, nLenOutString, nCnt, iCnt
m.nCnt = 0
m.hEnv = 0
m.hConn = 0
m.cInString = "DRIVER=SQL SERVER"
m.cOutString = Space(2048)
m.nLenOutString = 0
Local Array aServerList[1]
If SQLAllocHandle(1, 0, @hEnv) = 0
If SQLSetEnvAttr(m.hEnv, 200, 3, 0) = 0
If SQLAllocHandle(2, m.hEnv, @hConn) = 0
If SQLBrowseConnect(m.hConn, @cInString, Len(m.cInString), @cOutString, 2048, @nLenOutString) = 99
m.nCnt = Alines(aServerList, Strextract(m.cOutString, '{', '}'), .T., ',')
For m.iCnt = 1 To m.nCnt
Insert Into (m.tcTableName) Values ( aServerList[iCnt] )
Endfor
Endif
Endif
Endif
Endif
Endfunc
无意中找到了以前的一个C#程序,觉得好用发到这里以便和大家共享一下,其实这个代码我是抄别人过来的,原作者不详,觉得写的好就和大家共享一下了程序实现了用API来得到SqlServer服务器列表的功能,我觉得比用SQLDEMO组件更有意义,代码如下: /// <summary> /// 获取网内的数据库服务器名称 /// </summary> public class SqlLocator { [System.Runtime.InteropServices.DllImport("odbc32.dll")] private static extern short SQLAllocHandle(short hType, IntPtr inputHandle, 'B'
------解决方案--------------------
没试过,
看看!:)
-----------------
先装一个最新的SP2包,确保SQLDMO.dll是最新的。然后在C#中引用SqlDMO.dll,以下方法将局域网内的可用的SQL server列出到组合框中。
public long listSqlservers(ComboBox servers) { SQLDMO.Application sql = new SQLDMO.Application(); SQLDMO.NameList mynamelist = sql.Application.ListAvailableSQLServers(); long lCount = mynamelist.Count; for (int i=1; i < lCount + 1; i++) { string str = mynamelist.Item (i); if (str != null) if (servers.FindString (str) == -1) { servers.Items.Add (str);