Foxtable(狐表)用户栏目专家坐堂 → 本地SQL服务器名称如何通过程序获得?


  共有3675人关注过本帖树形打印复制链接

主题:本地SQL服务器名称如何通过程序获得?

帅哥哟,离线,有人找我吗?
whwsyscom
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:小狐 帖子:371 积分:4147 威望:0 精华:0 注册:2012/10/6 8:31:00
本地SQL服务器名称如何通过程序获得?  发帖心情 Post By:2014/12/21 10:44:00 [只看该作者]

本地SQL服务器名称如何通过程序获得?

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/12/21 10:49:00 [只看该作者]

 不需要获取,直接用计算机名,或者直接用ip地址127.0.0.1即可

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/12/21 11:03:00 [只看该作者]

 

全局代码

 

Public Class SqlLocator
 #Region "供使用API方式时使用"
 <System.Runtime.InteropServices.DllImport("odbc32.dll")> _
 Private Shared Function SQLAllocHandle(hType As Short, inputHandle As IntPtr, ByRef outputHandle As IntPtr) As Short
 End Function

 <System.Runtime.InteropServices.DllImport("odbc32.dll")> _
 Private Shared Function SQLSetEnvAttr(henv As IntPtr, attribute As Integer, valuePtr As IntPtr, strLength As Integer) As Short
 End Function

 <System.Runtime.InteropServices.DllImport("odbc32.dll")> _
 Private Shared Function SQLFreeHandle(hType As Short, handle As IntPtr) As Short
 End Function


 <System.Runtime.InteropServices.DllImport("odbc32.dll", CharSet := System.Runtime.InteropServices.CharSet.Ansi)> _
 Private Shared Function SQLBrowseConnect(hconn As IntPtr, inString As System.Text.StringBuilder, inStringLength As Short, outString As System.Text.StringBuilder, outStringLength As Short, ByRef outLengthNeeded As Short) As Short
 End Function

 Private Const SQL_HANDLE_ENV As Short = 1
 Private Const SQL_HANDLE_DBC As Short = 2
 Private Const SQL_ATTR_ODBC_VERSION As Integer = 200
 Private Const SQL_OV_ODBC3 As Integer = 3
 Private Const SQL_SUCCESS As Short = 0
 Private Const SQL_NEED_DATA As Short = 99
 Private Const DEFAULT_RESULT_SIZE As Short = 1024

 Private Const SQL_DRIVER_STR As String = "DRIVER=SQL SERVER"

 #End Region

 ''' <summary>
 ''' 禁止实例化
 ''' </summary>
 Private Sub New()
 End Sub

 ''' <summary>
 ''' 获取网内的数据库服务器名称
 ''' </summary>
 ''' <returns>服务器名称数组</returns>
 Public Shared Function GetLocalSqlServerNamesWithAPI() As String()
  Dim List As String = String.Empty
  Dim henv As IntPtr = IntPtr.Zero
  Dim hconn As IntPtr = IntPtr.Zero
  Dim inString As New System.Text.StringBuilder(SQL_DRIVER_STR)
  Dim outString As New System.Text.StringBuilder(DEFAULT_RESULT_SIZE)
  Dim inStringLength As Short = CShort(inString.Length)
  Dim lenNeeded As Short = 0
  Try
   If SQL_SUCCESS = SQLAllocHandle(SQL_HANDLE_ENV, henv, henv) Then
    If SQL_SUCCESS = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3, 0) Then
     If SQL_SUCCESS = SQLAllocHandle(SQL_HANDLE_DBC, henv, hconn) Then

      If SQL_NEED_DATA = SQLBrowseConnect(hconn, inString, inStringLength, outString, DEFAULT_RESULT_SIZE, lenNeeded) Then
       If DEFAULT_RESULT_SIZE < lenNeeded Then
        outString.Capacity = lenNeeded
        If SQL_NEED_DATA <> SQLBrowseConnect(hconn, inString, inStringLength, outString, lenNeeded, lenNeeded) Then
         Throw New ApplicationException("Unabled to aquire SQL Servers from ODBC driver.")
        End If
       End If
       List = outString.ToString()
       Dim start As Integer = List.IndexOf("{") + 1
       Dim len As Integer = list.IndexOf("}") - start
       If (start > 0) AndAlso (len > 0) Then
        list = list.Substring(start, len)
       Else
        list = String.Empty
       End If
      End If
     End If
    End If
   End If
  Catch
   list = String.Empty
  Finally

   If hconn <> IntPtr.Zero Then
    SQLFreeHandle(SQL_HANDLE_DBC, hconn)
   End If

   If henv <> IntPtr.Zero Then
    SQLFreeHandle(SQL_HANDLE_ENV, hconn)
   End If
  End Try

  Dim array As String() = Nothing

  If List.Length > 0 Then


   array = List.Split(","C)
  End If
  Return array
 End Function
End Class

 

 

调用代码

 

Dim ary() as string = SqlLocator.GetLocalSqlServerNamesWithAPI
For Each a As String In ary
    output.show(a)
Next


 回到顶部
帅哥哟,离线,有人找我吗?
lihe60
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:狐神 帖子:6887 积分:43621 威望:0 精华:0 注册:2009/3/2 14:07:00
  发帖心情 Post By:2014/12/21 21:17:00 [只看该作者]

以下是引用有点甜在2014-12-21 11:03:00的发言:

 

全局代码

 

Public Class SqlLocator
 #Region "供使用API方式时使用"
 <System.Runtime.InteropServices.DllImport("odbc32.dll")> _
 Private Shared Function SQLAllocHandle(hType As Short, inputHandle As IntPtr, ByRef outputHandle As IntPtr) As Short
 End Function

 <System.Runtime.InteropServices.DllImport("odbc32.dll")> _
 Private Shared Function SQLSetEnvAttr(henv As IntPtr, attribute As Integer, valuePtr As IntPtr, strLength As Integer) As Short
 End Function

 <System.Runtime.InteropServices.DllImport("odbc32.dll")> _
 Private Shared Function SQLFreeHandle(hType As Short, handle As IntPtr) As Short
 End Function


 <System.Runtime.InteropServices.DllImport("odbc32.dll", CharSet := System.Runtime.InteropServices.CharSet.Ansi)> _
 Private Shared Function SQLBrowseConnect(hconn As IntPtr, inString As System.Text.StringBuilder, inStringLength As Short, outString As System.Text.StringBuilder, outStringLength As Short, ByRef outLengthNeeded As Short) As Short
 End Function

 Private Const SQL_HANDLE_ENV As Short = 1
 Private Const SQL_HANDLE_DBC As Short = 2
 Private Const SQL_ATTR_ODBC_VERSION As Integer = 200
 Private Const SQL_OV_ODBC3 As Integer = 3
 Private Const SQL_SUCCESS As Short = 0
 Private Const SQL_NEED_DATA As Short = 99
 Private Const DEFAULT_RESULT_SIZE As Short = 1024

 Private Const SQL_DRIVER_STR As String = "DRIVER=SQL SERVER"

 #End Region

 ''' <summary>
 ''' 禁止实例化
 ''' </summary>
 Private Sub New()
 End Sub

 ''' <summary>
 ''' 获取网内的数据库服务器名称
 ''' </summary>
 ''' <returns>服务器名称数组</returns>
 Public Shared Function GetLocalSqlServerNamesWithAPI() As String()
  Dim List As String = String.Empty
  Dim henv As IntPtr = IntPtr.Zero
  Dim hconn As IntPtr = IntPtr.Zero
  Dim inString As New System.Text.StringBuilder(SQL_DRIVER_STR)
  Dim outString As New System.Text.StringBuilder(DEFAULT_RESULT_SIZE)
  Dim inStringLength As Short = CShort(inString.Length)
  Dim lenNeeded As Short = 0
  Try
   If SQL_SUCCESS = SQLAllocHandle(SQL_HANDLE_ENV, henv, henv) Then
    If SQL_SUCCESS = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3, 0) Then
     If SQL_SUCCESS = SQLAllocHandle(SQL_HANDLE_DBC, henv, hconn) Then

      If SQL_NEED_DATA = SQLBrowseConnect(hconn, inString, inStringLength, outString, DEFAULT_RESULT_SIZE, lenNeeded) Then
       If DEFAULT_RESULT_SIZE < lenNeeded Then
        outString.Capacity = lenNeeded
        If SQL_NEED_DATA <> SQLBrowseConnect(hconn, inString, inStringLength, outString, lenNeeded, lenNeeded) Then
         Throw New ApplicationException("Unabled to aquire SQL Servers from ODBC driver.")
        End If
       End If
       List = outString.ToString()
       Dim start As Integer = List.IndexOf("{") + 1
       Dim len As Integer = list.IndexOf("}") - start
       If (start > 0) AndAlso (len > 0) Then
        list = list.Substring(start, len)
       Else
        list = String.Empty
       End If
      End If
     End If
    End If
   End If
  Catch
   list = String.Empty
  Finally

   If hconn <> IntPtr.Zero Then
    SQLFreeHandle(SQL_HANDLE_DBC, hconn)
   End If

   If henv <> IntPtr.Zero Then
    SQLFreeHandle(SQL_HANDLE_ENV, hconn)
   End If
  End Try

  Dim array As String() = Nothing

  If List.Length > 0 Then


   array = List.Split(","C)
  End If
  Return array
 End Function
End Class

 

 

调用代码

 

Dim ary() as string = SqlLocator.GetLocalSqlServerNamesWithAPI
For Each a As String In ary
    output.show(a)
Next

有点甜,功深莫测。

 回到顶部
帅哥哟,离线,有人找我吗?
lihe60
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:狐神 帖子:6887 积分:43621 威望:0 精华:0 注册:2009/3/2 14:07:00
  发帖心情 Post By:2014/12/21 21:21:00 [只看该作者]

我请教一个问题,如何获取局域内指定SQL数据库的机器码?
[此贴子已经被作者于2014-12-21 21:22:18编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/12/21 21:47:00 [只看该作者]

 机器码指什么?电脑的?无法获取。

 回到顶部
帅哥哟,离线,有人找我吗?
lihe60
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:狐神 帖子:6887 积分:43621 威望:0 精华:0 注册:2009/3/2 14:07:00
  发帖心情 Post By:2014/12/21 22:10:00 [只看该作者]

以下是引用有点甜在2014-12-21 21:47:00的发言:
 机器码指什么?电脑的?无法获取。

帮助中的变量computerid


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  8楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/12/21 22:11:00 [只看该作者]

以下是引用lihe60在2014-12-21 22:10:00的发言:

帮助中的变量computerid

 

不能获取。


 回到顶部