如何用C#写的ASP.NET数据库操作类,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
下面是用C#写的ASP.NET数据库操作类:
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: 在此处添加构造函数逻辑 // } /// < summary> /// 公开方法DBConn,返回数据库连接 /// < /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> /// 根据查询语句从数据库检索数据 /// < /summary> /// < param name="strSelect">查询语句< /param> /// < param name="SqlConn">数据库连接< /param> /// < returns>有数据则返回DataSet对象,否则返回null< /returns> public DataSet Select(string SelectString, SqlConnection sqlConn) { strError = ""; SqlConnection conn; if (sqlConn == null) { conn = DBconn(); } else { conn = sqlConn; } try { //若数据库连接的当前状态是关闭的,则打开连接 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 = "数据检索失败:" + e.Message; return null; } finally { if (conn.State != ConnectionState.Closed) { conn.Close(); } } } /// < summary> /// 更新数据库 /// < /summary> /// < param name="UpdateString">Update Sql语句< /param> /// < param name="SqlConn">数据库连接< /param> /// < returns>更新成功返回true< /returns> public bool Update(string UpdateString, SqlConnection SqlConn) { return udiDataBase(UpdateString, SqlConn); } /// < summary> /// 从数据库中删除数据 /// < /summary> /// < param name="DeleteString">Delete Sql语句< /param> /// < param name="SqlConn">数据库连接< /param> /// < returns>删除成功返回true< /returns> public bool Delete(string DeleteString, SqlConnection SqlConn) { return udiDataBase(DeleteString, SqlConn); } /// < summary> /// 把数据插入数据库 /// < /summary> /// < param name="InsertString">Insert Sql语句< /param> /// < param name="SqlConn">数据库连接< /param> /// < returns>插入成功返回true< /returns> public bool Insert(string InsertString, SqlConnection SqlConn) { return udiDataBase(InsertString, SqlConn); } /// < summary> /// 根据Sql语句更新数据库 /// < /summary> /// < param name="UDIString">更新语句< /param> /// < param name="SqlConn">数据库连接< /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 = "更新数据库失败:" + e.Message; return false; } finally { if (conn.State != ConnectionState.Closed) { conn.Close(); } } } } }
-----------------------------
ASP.NET数据库操作类写好了,下面是两种调用方法
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#写的ASP.NET数据库类及调用方法。
看完上述内容,你们掌握如何用C#写的ASP.NET数据库操作类的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。