¿Qué método en C# puede obtener el nombre de dominio del host?
usando System;
usando System.Collections.Generic;
usando System.Text;
espacio de nombres Tecnología Zhicheng
{
clase SqlLocator
{
[System.Runtime.InteropServices.DllImport("odbc32.dll")]
SQLAllocHandle corto externo estático público (tipo h corto, mango de entrada IntPtr, mango de salida IntPtr);
[System.Runtime.InteropServices.DllImport("odbc32.dll")]
estático público externo corto SQLSetEnvAttr(IntPtr henv, int atributo, IntPtr valuePtr, int strLength);
[System.Runtime.InteropServices.DllImport("odbc32.dll")]
público estático externo short SQLFreeHandle(short hType, identificador IntPtr);
[System.Runtime.InteropServices.DllImport("odbc32.dll", CharSet = System.Runtime.InteropServices.CharSet.Ansi)]
público estático externo short SQLBrowseConnect(IntPtr hconn, System.Text.StringBuilder inString,
short inStringLength, System.Text.StringBuilder outString, short outStringLength,
out short outLengthNeeded);
public const short SQL_HANDLE_ENV = 1;
public const short SQL_HANDLE_DBC = 2
public const int SQL_ATTR_ODBC_VERSION = 200; const int SQL_OV_ODBC3 = 3;
public const short SQL_SUCCESS = 0;
public const short SQL_NEED_DATA = 99;
public const short DEFAULT_RESULT_SIZE = 1024;
public const string SQL_DRIVER_STR = "DRIVER=SQL SERVER";
public SqlLocator() { }
public static string[ ] GetServers()
{
lista de cadenas = string.Empty
IntPtr henv = IntPtr.Zero
IntPtr hconn; IntPtr.Zero;
System.Text.StringBuilder inString = nuevo System.Text.StringBuilder(SQL_DRIVER_STR);
System.Text.StringBuilder outString = nuevo System.Text.StringBuilder(DEFAULT_RESULT_SIZE );
short inStringLength = (short)inString.Length;
short lenNeeded = 0
intentar
{
if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_ENV, henv, out henv))
{
if (SQL_SUCCESS == SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (IntPtr)SQL_OV_ODBC3, 0))
{
if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_DBC, henv, out hconn))
{
if ( SQL_NEED_DATA == SQLBrowseConnect(hconn, inString, inStringLength, outString,
DEFAULT_RESULT_SIZE, out lenNeeded))
{
if (DEFAULT_RESULT_SIZE lt; lenN
eeded)
{
outString.Capacity = lenNeeded;
if (SQL_NEED_DATA != SQLBrowseConnect(hconn, inString, inStringLength, outString,
lenNeeded, out lenNeeded))
{
throw new ApplicationException("No se pueden adquirir servidores SQL desde el controlador ODBC."); p>
}
lista = outString.ToString();
int inicio = lista.IndexOf("{") 1; = list.IndexOf("}") - inicio;
if ((start gt; 0) amp; amp; (len gt; 0))
{
lista = lista.Substring(inicio, len);
}
else
{
lista = cadena.Vacío ;
}
}
}
}
}
}
captura
{
lista = cadena.Empty;
}
finalmente
{
if (hconn != IntPtr.Zero)
{
SQLFreeHandle(SQL_HANDLE_DBC, hconn
}
if (henv != IntPtr.Zero)
{
SQLFreeHandle(SQL_HANDLE_ENV, hconn
}
}
cadena[] matriz = nulo
if (list.Length gt; 0)
{
array = list.Split(', '
}
devolver matriz;
}
}
}
Lo hice