哪位高手指点一下,我的数据库操作类好象有问题,因为现在用户点击过多过快就容易出问题了
哪位高手指点一下,我的数据库操作类好象写的有问题,因为现在用户点击过多过快就容易出问题了,我的QQ是66377300,麻烦指点一下,混分的就不麻烦了,我用的是access2007的数据库,最好是加我QQ,我可以把情况说得更清楚一些
public class dbcon
{
public dbcon()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
protected static OleDbConnection con = new OleDbConnection();
protected static OleDbCommand cmd = new OleDbCommand();
public static OleDbConnection createConnection()
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["apath"]) + ";Jet OLEDB:Database Password=888888;");
return con;
}
public static void closecon()
{
if (con.State == ConnectionState.Open)
{
con.Close();
con.Dispose();
}
}
private static void openConnection()
{
if (con.State == ConnectionState.Closed)
{
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["apath"]) + ";Jet OLEDB:Database Password=888888;";
cmd.Connection = con;
try
{
con.Open();
}
catch
{
}
}
}
private static void closeConnection()
{
if (con.State == ConnectionState.Open)
{
con.Close();
con.Dispose();
cmd.Dispose();
}
}
public static bool executescala(string sqlstr)
{
int count = 0;
try
{
openConnection();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstr;
count = Convert.ToInt32(cmd.ExecuteScalar());
}
catch
{
safecode.catchshow();
}
finally
{
closeConnection();
}
if (count > 0)
{
return true;
}
else
{
return false;
}
}
public static bool executeupdate(string sqlstr) //用过了:判断是否执行sql更新语句
{
int updaterow = 0;
try
{
openConnection();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstr;
updaterow = Convert.ToInt32(cmd.ExecuteNonQuery());
}
catch
{
safecode.catchshow();
}
finally
{
closeConnection();
}
if (updaterow > 0)
{
return true;
}
else
{
return false;
}
}
public static object getscalar(string sqlstr) //返回首行首列
{
object obj = new object();
try
{
openConnection();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstr;
obj = cmd.ExecuteScalar();
}
catch
{
//throw new Exception(e.Message);
safecode.catchshow();
}
finally
{
closeConnection();
}
return obj;
}
public static OleDbDataReader getdatareader(string sqlstr)
{
OleDbDataReader dr = null;
try
{
openConnection();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstr;
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
try
{
dr.Dispose();
closeConnection();
throw new Exception(ex.Message);
}
catch { }
}
return dr;
}
public static DataSet getdataset(string sqlstr)
{
DataSet ds = new DataSet();
OleDbDataAdapter adp = new OleDbDataAdapter();
try
{
openConnection();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstr;
adp.SelectCommand = cmd;
adp.Fill(ds);
}
catch
{
safecode.catchshow();
}
finally
{
closeConnection();
}
return ds;
}
public static DataView getdataview(string sqlstr)
{
try
{
openConnection();
OleDbDataAdapter adp = new OleDbDataAdapter(sqlstr,con);
DataSet rs = new DataSet();
adp.Fill(rs);
return rs.Tables[0].DefaultView;
}
catch(Exception e)
{
throw new Exception(e.Message);
}
}
}