using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// sqlDB 的摘要说明
/// </summary>
public class sqlDB
{
//private static SqlConnection con;
protected static SqlConnection sqlcon=new SqlConnection();
protected static SqlCommand sqlcmd=new SqlCommand();
public sqlDB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private static void openConnection()
{
if (sqlcon.State == ConnectionState.Closed)
{
sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["VoteDBConnectionString"].ConnectionString;
sqlcmd.Connection = sqlcon;
try
{
sqlcon.Open();
}
catch (Exception ex)
{ throw new Exception(ex.Message); }
}
}
private static void closeConnection()
{
if (sqlcon.State == ConnectionState.Open)
{
sqlcon.Close();
sqlcon.Dispose();
sqlcmd.Dispose();
}
}
public static void ExecuteSQL(string SQLstring)
{
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqlcmd.ExecuteNonQuery();
}
catch (Exception ex)
{ throw new Exception(ex.Message); }
finally
{ closeConnection(); }
}
public static SqlDataReader SQLDataReader(string SQLstring)
{
SqlDataReader sqldr = null;
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqldr = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
try
{
sqldr.Close();
closeConnection();
}
catch { }
}
return sqldr;
}
public static void SQLDataReader(string SQLstring, ref SqlDataReader sqldr)
{
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqldr = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
try
{
if (sqldr != null && !sqldr.IsClosed)
sqldr.Close();
}
catch { }
finally
{
closeConnection();
}
}
}
public static DataSet SQLDataSet(string SQLstring)
{
DataSet sqlds = new DataSet();
SqlDataAdapter sqlda = new SqlDataAdapter();
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqlda.SelectCommand = sqlcmd;
sqlda.Fill(sqlds);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{ closeConnection(); }
return sqlds;
}
public static void SQLDataSet(string SQLstring, ref DataSet sqlds)
{
SqlDataAdapter sqlda = new SqlDataAdapter();
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqlda.SelectCommand = sqlcmd;
sqlda.Fill(sqlds);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{ closeConnection(); }
}
public static DataTable SQLDataTable(string SQLstring)
{
DataTable sqldt = new DataTable();
SqlDataAdapter sqlda = new SqlDataAdapter();
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqlda.SelectCommand = sqlcmd;
sqlda.Fill(sqldt);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
}
public static void SQLDataTable(string SQLstring, ref DataTable sqldt)
{
SqlDataAdapter sqlda = new SqlDataAdapter();
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqlda.SelectCommand = sqlcmd;
sqlda.Fill(sqldt);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{ closeConnection(); }
}
public static DataView SQLDataView(string SQLstring)
{
SqlDataAdapter sqlda = new SqlDataAdapter();
DataView sqldv = new DataView();
DataSet sqlds = new DataSet();
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqlda.SelectCommand = sqlcmd;
sqlda.Fill(sqlds);
sqldv = sqlds.Tables[0].DefaultView;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
return sqldv;
}
}
还不成熟,各位尽管批评,谢谢
[此贴子已经被作者于2006-11-19 15:20:23编辑过]