下面是代码,但是最近老是:连接池已满,无法获取连接的问题不知道是不是这个类写的那不对???
public class DataBase
{
public DataBase()
{
}
static string strcon = ConfigurationManager.AppSettings["ConnectionString"];
public static int getExecuteNonQuery(string SQLString, SqlParameter[] paramer)
{
using (SqlConnection connection = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
foreach (SqlParameter p in paramer)
{
cmd.Parameters.Add(p);
}
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException E)
{
throw new Exception(E.Message);
}
finally {
connection.Close();
}
}
}
}
public static int getExecuteNonQuery(string SQLString)
{
using (SqlConnection connection = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
throw new Exception(E.Message);
}
finally
{
connection.Close();
}
}
}
}
public static DataSet getds(string strsql, SqlParameter[] paramer)
{ using (SqlConnection connection = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand(strsql, connection))
{
DataSet ds = new DataSet();
try
{
foreach (SqlParameter p in paramer)
{
cmd.Parameters.Add(p);
}
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(cmd);
command.Fill(ds, "ds");
return ds;
}
catch (System.Data.SqlClient.SqlException ex)
{
connection.Close();
throw new Exception(ex.Message);
}
finally
{
connection.Close();
}
}
}
}
public static DataSet getds(string SQLString)
{
using (SqlConnection connection = new SqlConnection(strcon))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
return ds;
}
catch (System.Data.SqlClient.SqlException ex)
{
connection.Close();
throw new Exception(ex.Message);
}
finally
{
connection.Close();
}
}
}
public static DataSet getds(string SQLString, int currentpage,int pagesize)
{
using (SqlConnection connection = new SqlConnection(strcon))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, pagesize * (currentpage - 1), pagesize, "ds");
return ds;
}
catch (System.Data.SqlClient.SqlException ex)
{
connection.Close();
throw new Exception(ex.Message);
}
finally
{
connection.Close();
}
}
}
public static DataSet getds(string SQLString, SqlParameter[] paramer, int currentpage, int pagesize)
{ using (SqlConnection connection = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
DataSet ds = new DataSet();
try
{
foreach (SqlParameter p in paramer)
{
cmd.Parameters.Add(p);
}
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(cmd);
command.Fill(ds, pagesize * (currentpage - 1), pagesize, "ds");
return ds;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
throw new Exception(E.Message);
}
finally
{
connection.Close();
}
}
}
}
//执行存储过程
public static void execproc()
{
using (SqlConnection connection = new SqlConnection(strcon))
{ SqlCommand cmd = new SqlCommand();
try
{
cmd.Connection = connection;
cmd.CommandText = "getmidschid";
cmd.CommandType = CommandType.StoredProcedure;
connection.Open();
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
throw new Exception(E.Message);
}
finally
{
connection.Close();
}
}
}
}
public class DataBase
{
public DataBase()
{
}
static string strcon = ConfigurationManager.AppSettings["ConnectionString"];
public static int getExecuteNonQuery(string SQLString, SqlParameter[] paramer)
{
using (SqlConnection connection = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
foreach (SqlParameter p in paramer)
{
cmd.Parameters.Add(p);
}
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException E)
{
throw new Exception(E.Message);
}
finally {
connection.Close();
}
}
}
}
public static int getExecuteNonQuery(string SQLString)
{
using (SqlConnection connection = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
throw new Exception(E.Message);
}
finally
{
connection.Close();
}
}
}
}
public static DataSet getds(string strsql, SqlParameter[] paramer)
{ using (SqlConnection connection = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand(strsql, connection))
{
DataSet ds = new DataSet();
try
{
foreach (SqlParameter p in paramer)
{
cmd.Parameters.Add(p);
}
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(cmd);
command.Fill(ds, "ds");
return ds;
}
catch (System.Data.SqlClient.SqlException ex)
{
connection.Close();
throw new Exception(ex.Message);
}
finally
{
connection.Close();
}
}
}
}
public static DataSet getds(string SQLString)
{
using (SqlConnection connection = new SqlConnection(strcon))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
return ds;
}
catch (System.Data.SqlClient.SqlException ex)
{
connection.Close();
throw new Exception(ex.Message);
}
finally
{
connection.Close();
}
}
}
public static DataSet getds(string SQLString, int currentpage,int pagesize)
{
using (SqlConnection connection = new SqlConnection(strcon))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, pagesize * (currentpage - 1), pagesize, "ds");
return ds;
}
catch (System.Data.SqlClient.SqlException ex)
{
connection.Close();
throw new Exception(ex.Message);
}
finally
{
connection.Close();
}
}
}
public static DataSet getds(string SQLString, SqlParameter[] paramer, int currentpage, int pagesize)
{ using (SqlConnection connection = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
DataSet ds = new DataSet();
try
{
foreach (SqlParameter p in paramer)
{
cmd.Parameters.Add(p);
}
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(cmd);
command.Fill(ds, pagesize * (currentpage - 1), pagesize, "ds");
return ds;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
throw new Exception(E.Message);
}
finally
{
connection.Close();
}
}
}
}
//执行存储过程
public static void execproc()
{
using (SqlConnection connection = new SqlConnection(strcon))
{ SqlCommand cmd = new SqlCommand();
try
{
cmd.Connection = connection;
cmd.CommandText = "getmidschid";
cmd.CommandType = CommandType.StoredProcedure;
connection.Open();
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
throw new Exception(E.Message);
}
finally
{
connection.Close();
}
}
}
}
cmd.Parameters.AddWithValue("@BookID", bookID);
{
connection.Close();
cmd.Dispose();
}
其他没什么
http://topic.csdn.net/u/20090929/15/62ad727f-94b2-47ff-be8a-44667c51e574.html
{
Conn.Close();
}
Conn.Dispose();
不是吧
要想using有作用的话,要把return放到using大括号的后面吗?