ASP.NET(C#) 数据库操作类 SqlServerDataBase

  1 using System.Web.Security;
  2 using System.Web.UI;
  3 using System.Web.UI.WebControls;
  4 using System.Web.UI.WebControls.WebParts;
  5 using System.Web.UI.HtmlControls;
  6 using System.Data.SqlClient;
  7 
  8 namespace Mysqlserver
  9 {
 10     /// <summary>
 11     /// SqlServerDataBase 的摘要说明
 12     /// </summary>
 13     public class SqlServerDataBase
 14     {
 15         private string strError = null;
 16         private int intCount = 0;
 17         public SqlServerDataBase()
 18         {
 19             //
 20             // TOD 在此处添加构造函数逻辑
 21             //
 22         }
 23 
 24         /// <summary>
 25         /// 公开方法DBConn,返回数据库连接
 26         /// </summary>
 27         /// <returns></returns>
 28         public SqlConnection DBconn()
 29         {
 30             string strConn = "Server=(local);Database=GlobalMeetings;Uid=sa;pwd=";
 31             try
 32             {
 33                 return new SqlConnection(strConn);
 34             }
 35             catch (Exception)
 36             {
 37                 return null;
 38             }
 39         }
 40 
 41         /// <summary>
 42         /// 公开属性ErrorMessage,返回错误信息
 43         /// </summary>
 44         public string ErrorMessage
 45         {
 46             get
 47             {
 48                 return strError;
 49             }
 50         }
 51 
 52         /// <summary>
 53         /// 根据查询语句从数据库检索数据
 54         /// </summary>
 55         /// <param name="strSelect">查询语句</param>
 56         /// <param name="SqlConn">数据库连接</param>
 57         /// <returns>有数据则返回DataSet对象,否则返回null</returns>
 58         public DataSet Select(string SelectString, SqlConnection sqlConn)
 59         {
 60             strError = "";
 61             SqlConnection conn;
 62             if (sqlConn == null)
 63             {
 64                 conn = DBconn();
 65             }else
 66             {
 67                 conn = sqlConn;
 68             }
 69             try
 70             {
 71                 //若数据库连接的当前状态是关闭的,则打开连接
 72                 if (conn.State == ConnectionState.Closed)
 73                 {
 74                     conn.Open();
 75                 }
 76                 SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
 77                 SqlCommand selectCommand = new SqlCommand(SelectString, conn);
 78                 selectCommand.CommandType = CommandType.Text;
 79                 mySqlDataAdapter.SelectCommand = selectCommand;
 80                 DataSet myDS = new DataSet();
 81                 mySqlDataAdapter.Fill(myDS);
 82                 return myDS;
 83             }
 84             catch (Exception e)
 85             {
 86                 strError = "数据检索失败:" + e.Message;
 87                 return null;
 88             }
 89             finally
 90             {
 91                 if (conn.State != ConnectionState.Closed)
 92                 {
 93                     conn.Close();
 94                 }
 95             }
 96         }
 97 
 98         /// <summary>
 99         /// 更新数据库
100         /// </summary>
101         /// <param name="UpdateString">Update Sql语句</param>
102         /// <param name="SqlConn">数据库连接</param>
103         /// <returns>更新成功返回true</returns>
104         public bool Update(string UpdateString, SqlConnection SqlConn)
105         {
106             return udiDataBase(UpdateString, SqlConn);
107         }
108 
109         /// <summary>
110         /// 从数据库中删除数据
111         /// </summary>
112         /// <param name="DeleteString">Delete Sql语句</param>
113         /// <param name="SqlConn">数据库连接</param>
114         /// <returns>删除成功返回true</returns>
115         public bool Delete(string DeleteString, SqlConnection SqlConn)
116         {
117             return udiDataBase(DeleteString, SqlConn);
118         }
119 
120         /// <summary>
121         /// 把数据插入数据库
122         /// </summary>
123         /// <param name="InsertString">Insert Sql语句</param>
124         /// <param name="SqlConn">数据库连接</param>
125         /// <returns>插入成功返回true</returns>
126         public bool Insert(string InsertString, SqlConnection SqlConn)
127         {
128             return udiDataBase(InsertString, SqlConn);
129         }
130 
131         /// <summary>
132         /// 根据Sql语句更新数据库
133         /// </summary>
134         /// <param name="UDIString">更新语句</param>
135         /// <param name="SqlConn">数据库连接</param>
136         /// <returns>更新成功则返回true</returns>
137         public bool udiDataBase(string UDIString, SqlConnection SqlConn)
138         {
139             strError = "";
140             SqlConnection conn;
141             if (SqlConn == null)
142             {
143                 conn = DBconn();
144             }else
145             {
146                 conn = SqlConn;
147             }
148             try
149             {
150                 if (conn.State == ConnectionState.Closed)
151                 {
152                     conn.Open();
153                 }
154                 SqlCommand cmd = new SqlCommand(UDIString, conn);
155                 cmd.CommandType = CommandType.Text;
156                 intCount = cmd.ExecuteNonQuery();
157                 return !(intCount < 1);
158             }
159             catch (Exception e)
160             {
161                 strError = "更新数据库失败:" + e.Message;
162                 return false;
163             }
164             finally
165             {
166                 if (conn.State != ConnectionState.Closed)
167                 {
168                     conn.Close();
169                 }
170             }
171         }
172     }
173 }
View Code

//-----------------------------
// 调用方法一
//-----------------------------

    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);

//-----------------------------

// 调用方法二
//-----------------------------

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;
    }

ASP.NET(C#) 数据库操作类 SqlServerDataBase,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。