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;
}
}
}
查看全文
相关阅读:
机器学习:SVM(核函数、高斯核函数RBF)
机器学习:SVM(非线性数据分类:SVM中使用多项式特征和核函数SVC)
LeetCode566. Reshape the Matrix
LeetCode 128. Longest Consecutive Sequence
# 线程安全 & 线程安全函数 & 线程不安全函数
Linux进程状态
C++ 4种强制类型转换
TCP超时重传、滑动窗口、拥塞控制、快重传和快恢复
LeetCode 69. Sqrt(x)
LeetCode543. Diameter of Binary Tree
原文地址:https://www.cnblogs.com/gwazy/p/111127.html
最新文章
Readonly和disabled的区别 display:none和visible:hidden的区别
thinkphp框架中“关联操作”的完整定义详解
Eureka入门一(了解概念)
Ubuntu系统下各种报错杂烩(持续更新)
springBoot,springMVC, springCloud中常用注解 (持续更新)
线程安全问题(多方面考虑)
java中常见异常总汇,附解释
Dubbo分布式服务框架入门使用和跟事务共同使用时提供者(Provider)没能发布到注册中心解决方案
windows10不能修改hosts解决方案(亲测)
springboot中使用分页,文件上传,jquery的具体步骤(持续更新)
热门文章
CentOs 6 或 7 yum安装JDK1.8 (内含报 PYCURL ERROR 6
关于国际化时报org.springframework.context.NoSuchMessageException错,具体到No message found under code '你的键名' for locale 'zh_CN'.的解决方案
机器学习:决策树(决策树解决回归问题、决策树算法的局限性)
机器学习:决策树(CART 、决策树中的超参数)
机器学习:决策树(使用基尼系数划分节点数据集)
统计学基础(二):信息熵、基尼系数
机器学习:决策树(使用信息熵寻找最优划分)
机器学习:决策树(基本思想、信息熵、构建决策树的问题及思想)
机器学习:SVM(SVM 思想解决回归问题)
机器学习:SVM(scikit-learn 中的 RBF、RBF 中的超参数 γ)
Copyright © 2011-2022 走看看