這篇文章主要為大家展示了“ASP.NET數(shù)據(jù)庫操作類的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“ASP.NET數(shù)據(jù)庫操作類的示例分析”這篇文章吧。
創(chuàng)新互聯(lián)服務項目包括韶關網(wǎng)站建設、韶關網(wǎng)站制作、韶關網(wǎng)頁制作以及韶關網(wǎng)絡營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯(lián)網(wǎng)行業(yè)的解決方案,韶關網(wǎng)站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到韶關省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(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 { ////// SqlServerDataBase 的摘要說明 /// public class SqlServerDataBase { private string strError = null; private int intCount = 0; public SqlServerDataBase() { // // TODO: 在此處添加構造函數(shù)邏輯 // } ////// 公開方法DBConn,返回數(shù)據(jù)庫連接 /// ///public SqlConnection DBconn() { string strConn = "Server=(local);Database=GlobalMeetings;Uid=sa;pwd="; try { return new SqlConnection(strConn); } catch (Exception) { return null; } } /// /// 公開屬性ErrorMessage,返回錯誤信息 /// public string ErrorMessage { get { return strError; } } ////// 根據(jù)查詢語句從數(shù)據(jù)庫檢索數(shù)據(jù) /// /// 查詢語句 /// 數(shù)據(jù)庫連接 ///有數(shù)據(jù)則返回DataSet對象,否則返回null public DataSet Select(string SelectString, SqlConnection sqlConn) { strError = ""; SqlConnection conn; if (sqlConn == null) { conn = DBconn(); } else { conn = sqlConn; } try { //若數(shù)據(jù)庫連接的當前狀態(tài)是關閉的,則打開連接 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ù)庫 /// /// Update Sql語句 /// 數(shù)據(jù)庫連接 ///更新成功返回true public bool Update(string UpdateString, SqlConnection SqlConn) { return udiDataBase(UpdateString, SqlConn); } ////// 從數(shù)據(jù)庫中刪除數(shù)據(jù) /// /// Delete Sql語句 /// 數(shù)據(jù)庫連接 ///刪除成功返回true public bool Delete(string DeleteString, SqlConnection SqlConn) { return udiDataBase(DeleteString, SqlConn); } ////// 把數(shù)據(jù)插入數(shù)據(jù)庫 /// /// Insert Sql語句 /// 數(shù)據(jù)庫連接 ///插入成功返回true public bool Insert(string InsertString, SqlConnection SqlConn) { return udiDataBase(InsertString, SqlConn); } ////// 根據(jù)Sql語句更新數(shù)據(jù)庫 /// /// 更新語句 /// 數(shù)據(jù)庫連接 ///更新成功則返回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ù)庫失敗:" + e.Message; return false; } finally { if (conn.State != ConnectionState.Closed) { conn.Close(); } } } } }
以上是“ASP.NET數(shù)據(jù)庫操作類的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!