C#链接数据库

1.获取局域网内所有服务器
2.用户选择服务器
3.列举该服务器所有database
4.用户选取database
5.拼连接字符串
6.写入你的config文件

代码如下
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace BluePoint.ERM.AdminTool
{
/// <summary>
/// Summary description for SQLInfoEnumerator.
/// This class Enumerates a network for SQL Server instances and returns a list.
/// For a given SQL Server instance a list of all available databases is returned
/// For more information see
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcsqlbrowseconnect.asp
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09192002.asp
/// 
/// </summary>
public class SQLInfoEnumerator
{
#region ODBC32 external function definitions
[DllImport("odbc32.dll")]private static extern short SQLAllocHandle( short handleType, IntPtr inputHandle, out IntPtr outputHandlePtr );
[DllImport("odbc32.dll")]private static extern short SQLSetEnvAttr( IntPtr environmentHandle, int attribute, IntPtr valuePtr, int stringLength );
[DllImport("odbc32.dll")]private static extern short SQLFreeHandle( short hType, IntPtr Handle );
[DllImport("odbc32.dll",CharSet=CharSet.Ansi)]private static extern short SQLBrowseConnect( IntPtr handleConnection, StringBuilder inConnection, short stringLength, StringBuilder outConnection, short bufferLength, out short stringLength2Ptr );
#endregion
#region Constants
private const string SQL_DRIVER_STR = "DRIVER=SQL SERVER";
private const short SQL_SUCCESS = 0;
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_NEED_DATA = 99;
private const short DEFAULT_RESULT_SIZE = 1024;
private const string START_STR ="{";
private const string END_STR ="}";
#endregion

#region Field
/// <summary>
/// A string to hold the selected SQL Server
/// </summary>
string m_SQLServer;
/// <summary>
/// A string to hold the username
/// </summary>
string m_Username;
/// <summary>
/// A string to hold the password
/// </summary>
string m_Password;
/// <summary>
/// Property to set the SQL Server instance
/// </summary>
#endregion

#region property
public string SQLServer
{
set{m_SQLServer=value;}
}
/// <summary>
/// Property to set the Username
/// </summary>
public string Username
{
set{m_Username=value;}
}
/// <summary>
/// Property to set the Password
/// </summary>
public string Password
{
set{m_Password=value;}
}

/// <summary>
/// Enumerate the SQL Servers returning a list (if any exist)
/// </summary>
/// <returns></returns>
public string[] EnumerateSQLServers()
{
return RetrieveInformation(SQL_DRIVER_STR);
}
/// <summary>
/// Enumerate the specified SQL server returning a list of databases (if any exist)
/// </summary>
/// <returns></returns>
public string[] EnumerateSQLServersDatabases()
{
return RetrieveInformation(SQL_DRIVER_STR+";SERVER="+ m_SQLServer+";UID=" + m_Username +";PWD=" +m_Password);
}
#endregion

#region Function
/// <summary>
/// Enumerate for SQLServer/Databases based on the passed information it the string
/// The more information provided to SQLBrowseConnect the more granular it gets so
/// if only DRIVER=SQL SERVER passed then a list of all SQL Servers is returned
/// If DRIVER=SQL SERVER;Server=ServerName is passed then a list of all Databases on the
/// servers is returned etc
/// </summary>
/// <param name="InputParam">A valid string to query for</param>
/// <returns></returns>
private string[] RetrieveInformation(string InputParam)
{
IntPtr m_environmentHandle=IntPtr.Zero;
IntPtr m_connectionHandle = IntPtr.Zero;
StringBuilder inConnection = new StringBuilder(InputParam);
short stringLength= (short)inConnection.Length;
StringBuilder outConnection = new StringBuilder(DEFAULT_RESULT_SIZE);
short stringLength2Ptr= 0;

try
{
if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_ENV, m_environmentHandle, out m_environmentHandle))
{
if (SQL_SUCCESS == SQLSetEnvAttr(m_environmentHandle,SQL_ATTR_ODBC_VERSION,(IntPtr)SQL_OV_ODBC3,0))
{
if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_DBC, m_environmentHandle, out m_connectionHandle))
{
if (SQL_NEED_DATA == SQLBrowseConnect(m_connectionHandle, inConnection, stringLength, outConnection, DEFAULT_RESULT_SIZE, out stringLength2Ptr))
{
if (SQL_NEED_DATA != SQLBrowseConnect(m_connectionHandle, inConnection, stringLength, outConnection, DEFAULT_RESULT_SIZE, out stringLength2Ptr))
{
throw new ApplicationException("No Data Returned.");
}
}
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.ToString());
throw new ApplicationException("Cannot Locate SQL Server.");
}
finally
{
FreeConnection(m_connectionHandle);
FreeConnection(m_environmentHandle);
}
if (outConnection.ToString()!="")
{return ParseSQLOutConnection(outConnection.ToString());}
else{return null;}


}
/// <summary>
/// Parse an outConnection string returned from SQLBrowseConnect
/// </summary>
/// <param name="outConnection">string to parse</param>
/// <returns></returns>
private string[] ParseSQLOutConnection(string outConnection)
{
int m_Start = outConnection.IndexOf(START_STR) + 1;
int m_lenString = outConnection.IndexOf(END_STR) - m_Start;
if((m_Start>0) &&(m_lenString>0))
{outConnection = outConnection.Substring(m_Start,m_lenString);}
else
{ outConnection = string.Empty;}
return outConnection.Split(",".ToCharArray());
}
private void FreeConnection(IntPtr handleToFree)
{
if(handleToFree!= IntPtr.Zero)
SQLFreeHandle(SQL_HANDLE_DBC,handleToFree);
}
#endregion
}

}


接上帖

public static string[] GetAllServerNameOnNetwork() {
SQLInfoEnumerator sie = new SQLInfoEnumerator();
string[] sqlServers = sie.EnumerateSQLServers();
return sqlServers;
}  把这个string[] 加到你的combobox去

public static StringCollection GetAllDbNameInOneServer(string connectionStr) {
string mysql = "select name from master..sysdatabases order by name asc ";
SqlDataReader dr = DatabaseHelper.ExecuteReader( connectionStr, CommandType.Text, mysql, null);

StringCollection dbNames = new StringCollection();
while (dr.Read()) {
dbNames.Add(dr["name"].ToString()) ;
}
return dbNames;
}
得到所有数据库名. DatabaseHelper.ExecuteReader是包装微软的sqlhelper,你可以自己改写一下.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值