zoukankan
html css js c++ java
获取Sql服务器列表 (C#)
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);
}
}
类的代码
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_RESULT_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
==
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.
"
);
}
}
txt
=
outString.ToString();
int
start
=
txt.IndexOf(
"
{
"
)
+
1
;
int
len
=
txt.IndexOf(
"
}
"
)
-
start;
if
((start
>
0
)
&&
(len
>
0
))
{
txt
=
txt.Substring(start,len);
}
else
{
txt
=
string
.Empty;
}
}
}
}
}
}
catch
(Exception ex)
{
//
Throw away any error if we are not in debug mode
#if
(DEBUG)
MessageBox.Show(ex.Message,
"
Acquire SQL Servier List Error
"
);
#endif
txt
=
string
.Empty;
}
finally
{
if
(hconn
!=
IntPtr.Zero)
{
SQLFreeHandle(SQL_HANDLE_DBC,hconn);
}
if
(henv
!=
IntPtr.Zero)
{
SQLFreeHandle(SQL_HANDLE_ENV,hconn);
}
}
if
(txt.Length
>
0
)
{
retval
=
txt.Split(
"
,
"
.ToCharArray());
}
return
retval;
}
}
}
查看全文
相关阅读:
vscode conda 配置python环境(windows)
Linux分区只能分两个,无法安装双系统(解决)
离散傅里叶变换,逆变换(c语言)
vscode 配置task.json,执行多条指令
cmake出错:CMAKE_CXX_COMPILER设置后,提示没有设置,找不到make命令的可执行程序
产生makefiles文件后,make命令不可用
Kali 下载地址
vc6 保存文件卡住
fatal error LNK1169: one or more multiply defined symbols found
Google Chrome 退出清除浏览数据
原文地址:https://www.cnblogs.com/gwazy/p/111127.html
最新文章
C# 字符串处理函數大全
C#数组操作方法
C#泛型详解
SqlParameter的作用与用法
DBHelper
SQL字符串处理函数大全
Spring Boot使用事务
关于mybatis的<include refid="sqlBase" />
Spring Boot 集成 MyBatis--跟着慕课熊猫学
Spring Boot 使用 JPA--跟着慕课熊猫学
热门文章
Spring Boot数据源--跟着慕课熊猫学
Spring Boot使用jdbcTemplate--跟着慕课熊猫学
Spring Boot 跨域的实现--跟着慕课熊猫学
SpringBoot自定义配置--跟着慕课熊猫学
使用Swagger2自动测试--参考慕课熊猫
SpringBoot自动配置原理
向量、矩阵的范数/模(norm)
线性回归(python实现)
VSCode c/c++多文件编译环境配置(cmake+MinGW)
ImportError: DLL load failed: 找不到指定的模块。(conda配置的环境)解决
Copyright © 2011-2022 走看看