程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> Asp.net(c#)數據庫操作類

Asp.net(c#)數據庫操作類

編輯:ASP技巧

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();
                }
            }
        }
    }
}
 -----------------------------
兩種調用方法
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;
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved