以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  本地SQL服务器名称如何通过程序获得?  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=61872)

--  作者:whwsyscom
--  发布时间:2014/12/21 10:44:00
--  本地SQL服务器名称如何通过程序获得?
本地SQL服务器名称如何通过程序获得?
--  作者:有点甜
--  发布时间:2014/12/21 10:49:00
--  
 不需要获取,直接用计算机名,或者直接用ip地址127.0.0.1即可
--  作者:有点甜
--  发布时间: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
--  发布时间: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
--  发布时间:2014/12/21 21:21:00
--  
我请教一个问题,如何获取局域内指定SQL数据库的机器码?
[此贴子已经被作者于2014-12-21 21:22:18编辑过]

--  作者:有点甜
--  发布时间:2014/12/21 21:47:00
--  
 机器码指什么?电脑的?无法获取。
--  作者:lihe60
--  发布时间:2014/12/21 22:10:00
--  
以下是引用有点甜在2014-12-21 21:47:00的发言:
 机器码指什么?电脑的?无法获取。

帮助中的变量computerid


--  作者:有点甜
--  发布时间:2014/12/21 22:11:00
--  
以下是引用lihe60在2014-12-21 22:10:00的发言:

帮助中的变量computerid

 

不能获取。