作者:拍友2702936504 | 来源:互联网 | 2023-10-13 12:00
以下是我的sqlhelper代码,用完我都是关闭连接的 不懂为什么还报这个错!求大神们看看!
public class SqlHelper
{
//数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
public static readonly string ConnString = System.Configuration.ConfigurationSettings.AppSettings["SqlServerConnString"].ToString();
public SqlHelper() { }
static SqlConnection sqlConn;
private static SqlConnection GetConnection()
{
sqlConn = new SqlConnection(ConnString);
sqlConn.Open();
return sqlConn;
}
///
/// 增加、删除、修改的方法
///
///
///
返回ID
public static int ExecuteNoQuery2(string strsql)
{
int iCount = -1;
using (SqlConnection sqlconn = GetConnection())
{
if (sqlconn.State != ConnectionState.Open)
{
sqlconn.Open();
}
SqlCommand sqlcomm = new SqlCommand(strsql, sqlconn);
iCount = sqlcomm.ExecuteNonQuery();
if (iCount > 0)
{
sqlcomm.Parameters.Clear();
sqlcomm.CommandText = "Select @@identity as [MaxID]";
iCount = int.Parse(sqlcomm.ExecuteScalar().ToString());
}
}
if (sqlConn != null && sqlConn.State == ConnectionState.Open)
{
sqlConn.Close();
}
return iCount;
}
///
///
///
///
///
public static int ExecuteNoQuery(string strsql)
{
int iCount = 0;
using (SqlConnection sqlconn = GetConnection())
{
if (sqlconn.State != ConnectionState.Open)
{
sqlconn.Open();
}
SqlCommand sqlcomm = new SqlCommand(strsql, sqlconn);
iCount = sqlcomm.ExecuteNonQuery();
}
if (sqlConn != null && sqlConn.State == ConnectionState.Open)
{
sqlConn.Close();
}
return iCount;
}
///
/// 应用于删除修改增加
///
///
///
///
public static int ExecuteNoQuery(string strSql, SqlParameter[] sp)
{
using (SqlConnection conn = new SqlConnection(ConnString))
{
conn.Open();
SqlCommand sqlComm = new SqlCommand(strSql, conn);
sqlComm.Parameters.AddRange(sp);
int n = sqlComm.ExecuteNonQuery();
if (sqlConn != null && sqlConn.State == ConnectionState.Open)
{
sqlConn.Close();
}
return n;
}
}
///
///一般用于Count(*)
///
///
///
返回第一行第一列的值
public static int ExecuteScalar(string strSql)
{
object obj = new object();
try
{
using (SqlConnection sqlconn = GetConnection())
{
if (sqlconn.State != ConnectionState.Open)
{
sqlconn.Open();
}
SqlCommand sqlComm = new SqlCommand(strSql, sqlconn);
obj = sqlComm.ExecuteScalar();
}
}
catch (Exception ex)
{
}
finally
{
if (sqlConn != null && sqlConn.State == ConnectionState.Open)
{
sqlConn.Close();
}
}
if (obj == null)
{
return 0;
}
else
{
return (int)obj;
}
}
///
/// 返回数据集
///
///
///
返回DataSet对象
public static DataSet GetDataSet(string strSql)
{
sqlConn = GetConnection();
SqlDataAdapter sqlDA = new SqlDataAdapter(strSql, sqlConn);
DataSet ds = new DataSet();
sqlDA.Fill(ds);
if (sqlConn != null && sqlConn.State == ConnectionState.Open)
{
sqlConn.Close();
}
return ds;
}
///
/// 返回数据集
///
///
///
返回DataTable对象
public static DataTable GetDataTable(string strSql)
{
sqlConn = GetConnection();
DataTable ds = new DataTable();
try
{
SqlDataAdapter sqlDA = new SqlDataAdapter(strSql, sqlConn);
sqlDA.Fill(ds);
}
catch (Exception ex)
{
}
finally
{
if (sqlConn != null && sqlConn.State == ConnectionState.Open)
{
sqlConn.Close();
}
}
return ds;
}
///
/// 查询
///
///
///
返回DataReader对象
public static SqlDataReader ExecuteReader(string strSql)
{
sqlConn = GetConnection();
SqlCommand sqlComm = new SqlCommand(strSql, sqlConn);
SqlDataReader sdr= sqlComm.ExecuteReader();
return sdr;
}
///
/// 关闭数据库连接
///
public void CloseConn()
{
if (sqlConn != null && sqlConn.State == ConnectionState.Open)
{
sqlConn.Close();
}
}
}
7 个解决方案