赞
踩
1.查询sqlserver当前的连接数
SELECT * FROM
[Master].[dbo].[SYSPROCESSES] WHERE [DBID] IN ( SELECT
[DBID]
FROM
[Master].[dbo].[SYSDATABASES]
WHERE
NAME='DBName'
)
DBName是需要查看的数据库,然后查询出来的行数,就是当前的连接数
2.```csharp
//数据库链接字符串
string connectionString= “Server =” + serverAdr + “;User =” + userName + “;Pwd =” + passWord + “;Database =” + dataName;
//连接数据库对象
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
//query为查询语句
using (SqlCommand command = new SqlCommand(query, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
// 使用SqlDataReader对象读取数据
if (reader .HasRows)
{
reader .Read();
object obj = reader .GetValue(i);
}
// 关闭SqlDataReader对象
reader.Close();
}
}
// 关闭SqlConnection对象
connection.Close();
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。