這篇文章主要為大家展示了“ASP.NET數(shù)據(jù)庫(kù)操作類的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“ASP.NET數(shù)據(jù)庫(kù)操作類的示例分析”這篇文章吧。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請(qǐng)、網(wǎng)頁(yè)空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、薌城網(wǎng)站維護(hù)、網(wǎng)站推廣。具體如下:
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 { ////// SqlServerDataBase 的摘要說(shuō)明 /// public class SqlServerDataBase { private string strError = null; private int intCount = 0; public SqlServerDataBase() { // // TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } ////// 公開方法DBConn,返回?cái)?shù)據(jù)庫(kù)連接 /// ///public SqlConnection DBconn() { string strConn = "Server=(local);Database=GlobalMeetings;Uid=sa;pwd="; try { return new SqlConnection(strConn); } catch (Exception) { return null; } } /// /// 公開屬性ErrorMessage,返回錯(cuò)誤信息 /// public string ErrorMessage { get { return strError; } } ////// 根據(jù)查詢語(yǔ)句從數(shù)據(jù)庫(kù)檢索數(shù)據(jù) /// /// 查詢語(yǔ)句 /// 數(shù)據(jù)庫(kù)連接 ///有數(shù)據(jù)則返回DataSet對(duì)象,否則返回null public DataSet Select(string SelectString, SqlConnection sqlConn) { strError = ""; SqlConnection conn; if (sqlConn == null) { conn = DBconn(); } else { conn = sqlConn; } try { //若數(shù)據(jù)庫(kù)連接的當(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ù)檢索失?。? + e.Message; return null; } finally { if (conn.State != ConnectionState.Closed) { conn.Close(); } } } ////// 更新數(shù)據(jù)庫(kù) /// /// Update Sql語(yǔ)句 /// 數(shù)據(jù)庫(kù)連接 ///更新成功返回true public bool Update(string UpdateString, SqlConnection SqlConn) { return udiDataBase(UpdateString, SqlConn); } ////// 從數(shù)據(jù)庫(kù)中刪除數(shù)據(jù) /// /// Delete Sql語(yǔ)句 /// 數(shù)據(jù)庫(kù)連接 ///刪除成功返回true public bool Delete(string DeleteString, SqlConnection SqlConn) { return udiDataBase(DeleteString, SqlConn); } ////// 把數(shù)據(jù)插入數(shù)據(jù)庫(kù) /// /// Insert Sql語(yǔ)句 /// 數(shù)據(jù)庫(kù)連接 ///插入成功返回true public bool Insert(string InsertString, SqlConnection SqlConn) { return udiDataBase(InsertString, SqlConn); } ////// 根據(jù)Sql語(yǔ)句更新數(shù)據(jù)庫(kù) /// /// 更新語(yǔ)句 /// 數(shù)據(jù)庫(kù)連接 ///更新成功則返回true 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ù)庫(kù)失敗:" + e.Message; return false; } finally { if (conn.State != ConnectionState.Closed) { conn.Close(); } } } } }
以上是“ASP.NET數(shù)據(jù)庫(kù)操作類的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!