Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write ASP.NET database operation class with C #

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to use C # to write the ASP.NET database operation class, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

The following is the ASP.NET database operation class written in C #:

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>

/ / Summary description of SqlServerDataBase /

< /summary>

Public class SqlServerDataBase {private string strError = null; private int intCount = 0; public SqlServerDataBase () {/ TODO: add constructor logic / /} / / here

< summary>

/ / expose the method DBConn, which returns the database connection /

< /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>

/ / expose the property ErrorMessage and return an error message /

< /summary>

Public string ErrorMessage {get {return strError;}} /

< summary>

/ / retrieve data from the database according to the query statement / /

< /summary>

/ / /

< param name="strSelect">

Query statement

< /param>

/ / /

< param name="SqlConn">

Database connection

< /param>

/ / /

< returns>

DataSet object is returned if there is data, otherwise null is returned

< /returns>

Public DataSet Select (string SelectString, SqlConnection sqlConn) {strError = ""; SqlConnection conn; if (sqlConn = = null) {conn = DBconn ();} else {conn = sqlConn } try {/ / if the current state of the database connection is closed, open the connection 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 = "data retrieval failed:" + e.Message; return null } finally {if (conn.State! = ConnectionState.Closed) {conn.Close ();} / /

< summary>

/ Update database /

< /summary>

/ / /

< param name="UpdateString">

Update Sql statement

< /param>

/ / /

< param name="SqlConn">

Database connection

< /param>

/ / /

< returns>

True is returned if the update is successful

< /returns>

Public bool Update (string UpdateString, SqlConnection SqlConn) {return udiDataBase (UpdateString, SqlConn);} / /

< summary>

/ / Delete data from the database / /

< /summary>

/ / /

< param name="DeleteString">

Delete Sql statement

< /param>

/ / /

< param name="SqlConn">

Database connection

< /param>

/ / /

< returns>

True is returned if deleted successfully

< /returns>

Public bool Delete (string DeleteString, SqlConnection SqlConn) {return udiDataBase (DeleteString, SqlConn);} / /

< summary>

/ / insert data into the database / /

< /summary>

/ / /

< param name="InsertString">

Insert Sql statement

< /param>

/ / /

< param name="SqlConn">

Database connection

< /param>

/ / /

< returns>

True is returned after successful insertion.

< /returns>

Public bool Insert (string InsertString, SqlConnection SqlConn) {return udiDataBase (InsertString, SqlConn);} / /

< summary>

/ / update the database according to the Sql statement /

< /summary>

/ / /

< param name="UDIString">

Update statement

< /param>

/ / /

< param name="SqlConn">

Database connection

< /param>

/ / /

< returns>

True is returned if the update is successful.

< /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 = "failed to update database:" + e.message; return false } finally {if (conn.State! = ConnectionState.Closed) {conn.Close ();}}

-

Now that the ASP.NET database operation class is written, here are two invocation methods

1 、

String strUserPsw = UserPsw.Text.Trim (); string UserPassWord = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (strUserPsw, "MD5"); / / md5 encryption 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;}

The above introduces the ASP.NET database class and calling method written in C #.

After reading the above, have you mastered how to write the ASP.NET database operation class in C #? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report