Get list of SQL Server (C #)

zhaozj2021-02-16  106

?

How to achieve this feature, everyone will definitely know, put it up to leave a backup to you:

Private void button1_click_1 (Object sender, system.eventargs e) ... {string [] servers = dbgrep.sqllocator.getServers (); foreach (string s in servers) ... {this.listbox1.items.add (s) }}} Class code

using System; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace DBGrep ... {public class SqlLocator ... {[DllImport ( "odbc32.dll")] private static extern short SQLAllocHandle (short hType, IntPtr inputHandle, out IntPtr outputHandle); [DllImport ( "odbc32.dll")] private static extern short SQLSetEnvAttr (IntPtr henv, int attribute, IntPtr valuePtr, int strLength); [DllImport ( "odbc32.dll") ] private static extern short SQLFreeHandle (short hType, IntPtr handle); [DllImport ( "odbc32.dll", CharSet = CharSet.Ansi)] private static extern short SQLBrowseConnect (IntPtr hconn, StringBuilder inString, short inStringLength, StringBuilder outString, short outStringLength , out short outLengthNeeded); private const short SQL_HANDLE_ENV = 1; private const short SQL_HANDLE_DBC = 2; private const int SQL_ATTR_ODBC_VERSION = 200; private const int SQL_OV_ODBC3 = 3; private const short SQL_SUCCESS = 0; private const short SQL_NEED_DATA = 99; private const Short default_re SULT_SIZE = 1024; private const string SQL_DRIVER_STR = "DRIVER = SQL SERVER"; private SqlLocator () ... {} public static string [] GetServers () ... {string [] retval = null; string txt = string.Empty ; IntPtr henv = IntPtr.Zero; IntPtr hconn = IntPtr.Zero; StringBuilder inString = new StringBuilder (SQL_DRIVER_STR); StringBuilder outString = new StringBuilder (DEFAULT_RESULT_SIZE); short inStringLength = (short) inString.Length; short lenNeeded = 0; try. .. {if (sql_success == sqlallochandle (SQL_HANDLE_ENV, HENV, OUT HENV)) ... {IF (SQL_SUCCESS ==

转载请注明原文地址:https://www.9cbs.com/read-10398.html

New Post(0)