您好,登錄后才能下訂單哦!
如何用C#寫(xiě)的ASP.NET數(shù)據(jù)庫(kù)操作類,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
下面是用C#寫(xiě)的ASP.NET數(shù)據(jù)庫(kù)操作類:
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 的摘要說(shuō)明 /// < /summary> public class SqlServerDataBase { private string strError = null; private int intCount = 0; public SqlServerDataBase() { // // TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } /// < summary> /// 公開(kāi)方法DBConn,返回?cái)?shù)據(jù)庫(kù)連接 /// < /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> /// 公開(kāi)屬性ErrorMessage,返回錯(cuò)誤信息 /// < /summary> public string ErrorMessage { get { return strError; } } /// < summary> /// 根據(jù)查詢語(yǔ)句從數(shù)據(jù)庫(kù)檢索數(shù)據(jù) /// < /summary> /// < param name="strSelect">查詢語(yǔ)句< /param> /// < param name="SqlConn">數(shù)據(jù)庫(kù)連接< /param> /// < returns>有數(shù)據(jù)則返回DataSet對(duì)象,否則返回null< /returns> 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)閉的,則打開(kā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ù)檢索失?。?quot; + e.Message; return null; } finally { if (conn.State != ConnectionState.Closed) { conn.Close(); } } } /// < summary> /// 更新數(shù)據(jù)庫(kù) /// < /summary> /// < param name="UpdateString">Update Sql語(yǔ)句< /param> /// < param name="SqlConn">數(shù)據(jù)庫(kù)連接< /param> /// < returns>更新成功返回true< /returns> public bool Update(string UpdateString, SqlConnection SqlConn) { return udiDataBase(UpdateString, SqlConn); } /// < summary> /// 從數(shù)據(jù)庫(kù)中刪除數(shù)據(jù) /// < /summary> /// < param name="DeleteString">Delete Sql語(yǔ)句< /param> /// < param name="SqlConn">數(shù)據(jù)庫(kù)連接< /param> /// < returns>刪除成功返回true< /returns> public bool Delete(string DeleteString, SqlConnection SqlConn) { return udiDataBase(DeleteString, SqlConn); } /// < summary> /// 把數(shù)據(jù)插入數(shù)據(jù)庫(kù) /// < /summary> /// < param name="InsertString">Insert Sql語(yǔ)句< /param> /// < param name="SqlConn">數(shù)據(jù)庫(kù)連接< /param> /// < returns>插入成功返回true< /returns> public bool Insert(string InsertString, SqlConnection SqlConn) { return udiDataBase(InsertString, SqlConn); } /// < summary> /// 根據(jù)Sql語(yǔ)句更新數(shù)據(jù)庫(kù) /// < /summary> /// < param name="UDIString">更新語(yǔ)句< /param> /// < param name="SqlConn">數(shù)據(jù)庫(kù)連接< /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ù)庫(kù)失?。?quot; + e.Message; return false; } finally { if (conn.State != ConnectionState.Closed) { conn.Close(); } } } } }
-----------------------------
ASP.NET數(shù)據(jù)庫(kù)操作類寫(xiě)好了,下面是兩種調(diào)用方法
1、
string strUserPsw = UserPsw.Text.Trim(); string UserPassWord = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strUserPsw, "MD5");//md5加密 SqlServerDataBase obj = new SqlServerDataBase(); obj.Insert("insert into asUserInfo (UserName,UserPassword,Question,Answer,CreateTime) values('" + UserName.Text.Trim() + "','" + UserPassword + "','" + Question.Text.Trim() + "','" + Answer.Text.Trim() + "','" + DateTime.Now.ToString() + "' )", null);
2、
private bool IsUsernameExist(string strUsername) { bool bRet = true; SqlServerDataBase db = new SqlServerDataBase(); DataSet ds = db.Select("select * from asUserInfo where UserName = '" + strUsername + "'", null); if (ds == null ds.Tables.Count == 0 ds.Tables[0].Rows.Count == 0) { bRet = false; } else { bRet = true; } return bRet; }
以上就介紹了用C#寫(xiě)的ASP.NET數(shù)據(jù)庫(kù)類及調(diào)用方法。
看完上述內(nèi)容,你們掌握如何用C#寫(xiě)的ASP.NET數(shù)據(jù)庫(kù)操作類的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。