ASP.NET中怎么實現(xiàn)一個數(shù)據(jù)庫操作類,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
具體如下:
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; namespace Mysqlserver { /// <summary> /// SqlServerDataBase 的摘要說明 /// </summary> public class SqlServerDataBase { private string strError = null; private int intCount = 0; public SqlServerDataBase() { // // TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } /// <summary> /// 公開方法DBConn,返回數(shù)據(jù)庫連接 /// </summary> /// <returns></returns> public SqlConnection DBconn() { string strConn = "Server=(local);Database=GlobalMeetings;Uid=sa;pwd="; try { return new SqlConnection(strConn); } catch (Exception) { return null; } } /// <summary> /// 公開屬性ErrorMessage,返回錯誤信息 /// </summary> public string ErrorMessage { get { return strError; } } /// <summary> /// 根據(jù)查詢語句從數(shù)據(jù)庫檢索數(shù)據(jù) /// </summary> /// <param name="strSelect">查詢語句</param> /// <param name="SqlConn">數(shù)據(jù)庫連接</param> /// <returns>有數(shù)據(jù)則返回DataSet對象,否則返回null</returns> public DataSet Select(string SelectString, SqlConnection sqlConn) { strError = ""; SqlConnection conn; if (sqlConn == null) { conn = DBconn(); } else { conn = sqlConn; } try { //若數(shù)據(jù)庫連接的當(dāng)前狀態(tài)是關(guān)閉的,則打開連接 if (conn.State == ConnectionState.Closed) { conn.Open(); } SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(); SqlCommand selectCommand = new SqlCommand(SelectString, conn); selectCommand.CommandType = CommandType.Text; mySqlDataAdapter.SelectCommand = selectCommand; DataSet myDS = new DataSet(); mySqlDataAdapter.Fill(myDS); return myDS; } catch (Exception e) { strError = "數(shù)據(jù)檢索失?。?quot; + e.Message; return null; } finally { if (conn.State != ConnectionState.Closed) { conn.Close(); } } } /// <summary> /// 更新數(shù)據(jù)庫 /// </summary> /// <param name="UpdateString">Update Sql語句</param> /// <param name="SqlConn">數(shù)據(jù)庫連接</param> /// <returns>更新成功返回true</returns> public bool Update(string UpdateString, SqlConnection SqlConn) { return udiDataBase(UpdateString, SqlConn); } /// <summary> /// 從數(shù)據(jù)庫中刪除數(shù)據(jù) /// </summary> /// <param name="DeleteString">Delete Sql語句</param> /// <param name="SqlConn">數(shù)據(jù)庫連接</param> /// <returns>刪除成功返回true</returns> public bool Delete(string DeleteString, SqlConnection SqlConn) { return udiDataBase(DeleteString, SqlConn); } /// <summary> /// 把數(shù)據(jù)插入數(shù)據(jù)庫 /// </summary> /// <param name="InsertString">Insert Sql語句</param> /// <param name="SqlConn">數(shù)據(jù)庫連接</param> /// <returns>插入成功返回true</returns> public bool Insert(string InsertString, SqlConnection SqlConn) { return udiDataBase(InsertString, SqlConn); } /// <summary> /// 根據(jù)Sql語句更新數(shù)據(jù)庫 /// </summary> /// <param name="UDIString">更新語句</param> /// <param name="SqlConn">數(shù)據(jù)庫連接</param> /// <returns>更新成功則返回true</returns> public bool udiDataBase(string UDIString, SqlConnection SqlConn) { strError = ""; SqlConnection conn; if (SqlConn == null) { conn = DBconn(); } else { conn = SqlConn; } try { if (conn.State == ConnectionState.Closed) { conn.Open(); } SqlCommand cmd = new SqlCommand(UDIString, conn); cmd.CommandType = CommandType.Text; intCount = cmd.ExecuteNonQuery(); return !(intCount < 1); } catch (Exception e) { strError = "更新數(shù)據(jù)庫失敗:" + e.Message; return false; } finally { if (conn.State != ConnectionState.Closed) { conn.Close(); } } } } }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。
分享題目:ASP.NET中怎么實現(xiàn)一個數(shù)據(jù)庫操作類-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://muchs.cn/article18/hiigp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、靜態(tài)網(wǎng)站、動態(tài)網(wǎng)站、面包屑導(dǎo)航、網(wǎng)站收錄、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容