本帖最后由 cenbz 于 2014-8-1 11:43 编辑
之前给同学做过sql server 的配置向导,原因是做了个积分管理的应用程序,程序和数据库的连接语句是加密后放到一个特殊文件里的,因为管理程序要到别的电脑上运行,所以数据的连接要重新配置。配置程序的主要核心是如何获取本地电脑和局域网内的所有可见数据库服务器。在网上有个大牛给出了个示例代码,具体连接没记下。在这里给出他的获取sql服务器的代码:[C#] 纯文本查看 复制代码 #region 声明和定义
[System.Runtime.InteropServices.DllImport("odbc32.dll")]
private static extern short SQLAllocHandle(short hType, IntPtr inputHandle, out IntPtr outputHandle);
[System.Runtime.InteropServices.DllImport("odbc32.dll")]
private static extern short SQLSetEnvAttr(IntPtr henv, int attribute, IntPtr valuePtr, int strLength);
[System.Runtime.InteropServices.DllImport("odbc32.dll")]
private static extern short SQLFreeHandle(short hType, IntPtr handle);
[System.Runtime.InteropServices.DllImport("odbc32.dll", CharSet = System.Runtime.InteropServices.CharSet.Ansi)]
private static extern short SQLBrowseConnect(IntPtr hconn, System.Text.StringBuilder inString,
short inStringLength, System.Text.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_RESULT_SIZE = 1024;
private const string SQL_DRIVER_STR = "DRIVER=SQL SERVER";
#endregion
[C#] 纯文本查看 复制代码 /// <summary>
/// 获取本地上或局域网内的数据库
/// </summary>
/// <returns></returns>
public static string[] GetServers()
{
string list = 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 == 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 < lenNeeded)
{
outString.Capacity = lenNeeded;
if (SQL_NEED_DATA != SQLBrowseConnect(hconn, inString, inStringLength, outString, lenNeeded, out lenNeeded))
{
throw new ApplicationException("Unabled to aquire SQL Servers from ODBC driver.");
}
}
list = outString.ToString();
int start = list.IndexOf('{') + 1;
int len = list.IndexOf('}') - start;
if ((start > 0) && (len > 0))
{
list = list.Substring(start, len);
}
else
{
list = string.Empty;
}
}
}
}
}
}
catch
{
list = string.Empty;
}
finally
{
if (hconn != IntPtr.Zero)
{
SQLFreeHandle(SQL_HANDLE_DBC, hconn);
}
if (henv != IntPtr.Zero)
{
SQLFreeHandle(SQL_HANDLE_ENV, hconn);
}
}
string[] array = null;
if (list.Length > 0)
{
array = list.Split(',');
}
return array;
}
//GetServers()这个函数返回服务器名称的字符串数组
我们可以使用 comboBox控件来提供选择:comboBox1.Items.AddRange(GetServers());
配置向导的界面参考下面图示,其他的功能代码不难。 这里获取服务器的数据库的SQL语句是:"select * from sysdatabases where sid<>0x01" ////where sid<>0x01是不选择自带的系统数据库。
|