public class DB { public static OleDbConnection Conn; public static string ConnString;//連接字符串 public DB() { // // TODO: 在此處添加構造函數(shù)邏輯 // } public static OleDbConnection Getconn() { ConnString = "Provider=Microsoft.Jet.OLEDB.4
public class DB
{
public static OleDbConnection Conn;
public static string ConnString;//連接字符串
public DB()
{
//
// TODO: 在此處添加構造函數(shù)邏輯
//
}
public static OleDbConnection Getconn()
{
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ConnectionString"].ToString());
Conn = new OleDbConnection(ConnString);
//if (Conn.State.Equals(ConnectionState.Closed))
//{
// Conn.Open();
//}
if (Conn == null)
{
Conn = new OleDbConnection(ConnString);
Conn.Open();
}
else if (Conn.State == System.Data.ConnectionState.Closed)
{
Conn.Open();
}
else if (Conn.State == System.Data.ConnectionState.Broken)
{
Conn.Close();
Conn.Open();
}
return Conn;
}
//=================================================
//功能描述:關閉數(shù)據(jù)庫
//時間:2010.11.10
//=================================================
private static void closeConnection()
{
OleDbConnection conn = DB.Getconn();
OleDbCommand cmd = new OleDbCommand();
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
cmd.Dispose();
}
}
//=================================================
//功能描述:執(zhí)行SQL語句
//輸入?yún)?shù):sql,查詢的SQL語句
//時間:2010.11.10
//=================================================
public static void execnonsql(string sql)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
OleDbCommand com = new OleDbCommand(sql, conn);
com.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:獲取DATASET
//輸入?yún)?shù):sql,查詢的SQL語句
//返回值:DataSet
//時間:2010.11.10
//=================================================
public static DataSet getdataset(string sql)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
OleDbDataAdapter adp = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
adp.Fill(ds, "ds");
return ds;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:獲取DATASET1
//輸入?yún)?shù):sql,查詢的SQL語句
//返回值:DataSet
//時間:2010.11.10
//=================================================
public static DataSet select(string sql, string tablename)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
OleDbDataAdapter adp = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
adp.Fill(ds, tablename);
return ds;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:獲取某個字段數(shù)據(jù)
//輸入?yún)?shù):sql,查詢的SQL語句
//返回值:hang
//時間:2010.11.10
//=================================================
public static string FindString(string sql)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
OleDbCommand com = new OleDbCommand(sql, conn);
string hang = Convert.ToString(com.ExecuteScalar());
return hang;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:對DATAGRIG進行數(shù)據(jù)綁定,無排序
//輸入?yún)?shù):sql,查詢的SQL語句;dg,需要綁定的DATAGRID控件
//返回值:無
//時間:2010.11.10
//=================================================
public static void binddatagrid(string sql, DataGrid dg)
{
try
{
DataSet ds = getdataset(sql);
dg.DataSource = ds.Tables[0].DefaultView;
dg.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:對DropDownList進行數(shù)據(jù)綁定,無排序
//輸入?yún)?shù):sql,查詢的SQL語句;dg,需要綁定的DATAGRID控件
//返回值:無
//時間:2010.11.10
//=================================================
public static void bindDropDownList(string sql, DropDownList dl, string class_name, string id)
{
try
{
DataSet ds = getdataset(sql);
dl.DataSource = ds.Tables[0].DefaultView;
dl.DataTextField = class_name;
dl.DataValueField = id;
dl.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:對RadioButtonList進行數(shù)據(jù)綁定,無排序
//輸入?yún)?shù):sql,查詢的SQL語句;dg,需要綁定的DATAGRID控件
//返回值:無
//時間:2010.11.10
//=================================================
public static void bindRadioButtonList(string sql, RadioButtonList rl, string class_name, string id)
{
try
{
DataSet ds = getdataset(sql);
rl.DataSource = ds.Tables[0].DefaultView;
rl.DataTextField = class_name;
rl.DataValueField = id;
rl.SelectedIndex = 0;
rl.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:對GridView進行數(shù)據(jù)綁定,無排序
//輸入?yún)?shù):sql,查詢的SQL語句;dg,需要綁定的DATAGRID控件
//返回值:無
//時間:2010.11.10
//=================================================
public static void bindGridView(string sql, GridView dg)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
DataSet ds = getdataset(sql);
dg.DataSource = ds.Tables[0].DefaultView;
dg.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:對datalist進行數(shù)據(jù)綁定,無排序
//輸入?yún)?shù):sql,查詢的SQL語句;dl,需要綁定的datalist控件
//返回值:無
//時間:2010.11.10
//=================================================
public static void binddatalist(string sql, DataList dl)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
DataSet ds = getdataset(sql);
dl.DataSource = ds.Tables[0].DefaultView;
dl.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:對repeater進行數(shù)據(jù)綁定,無排序
//輸入?yún)?shù):sql,查詢的SQL語句;dl,需要綁定的repeater控件
//返回值:無
//時間:2010.11.10
//=================================================
public static void bindrepeater(string sql, Repeater rp)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
DataSet ds = getdataset(sql);
rp.DataSource = ds.Tables[0].DefaultView;
rp.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:對listbox進行數(shù)據(jù)綁定
//輸入?yún)?shù):sql,查詢的SQL語句;listb,需要綁定的listbox控件
//返回值:無
//時間:2010.11.10
//=================================================
public static void bindlistbox(string sql, ListBox listb, string class_name, string id)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
DataSet ds = getdataset(sql);
listb.DataSource = ds.Tables[0].DefaultView;
listb.DataTextField = class_name;
listb.DataValueField = id;
listb.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
///
/// 返回 HTML 字符串的編碼結果
///
/// 字符串
///
public static string HtmlEncode(string str)
{
return HttpUtility.HtmlEncode(str);
}
///
/// 返回 HTML 字符串的解碼結果
///
/// 字符串
///
public static string HtmlDecode(string str)
{
return HttpUtility.HtmlDecode(str);
}
///
/// 檢測是否有Sql危險字符
///
/// 要判斷字符串
///
public static bool IsSafeSqlString(string str)
{
return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
}
///
/// 檢測用戶登錄。
///
///
///
public static string UserCheck(string username, string userpass)
{
string strsql = "select count(*) from Member where mem_Name='" + username + "' and mem_Password='" + userpass + "'";
OleDbConnection conn = DB.Getconn();
OleDbCommand com = new OleDbCommand(strsql, conn);
string hang = Convert.ToString(com.ExecuteScalar());
return hang;
}
}
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com