特色栏目

ASP源码

PHP源码

.NET源码

JSP源码

游戏频道
专题合集
关闭菜单
首页> ASP教程> Asp.net(c#)数据库操作类

Asp.net(c#)数据库操作类

时间:2009-06-25 17:38:56 作者:互联网

using System;
using Sy***m.Data;
using Sy***m.Configuration;
using Sy***m.Web;
using Sy***m.Web.Security;
using Sy***m.Web.UI;
using Sy***m.Web.UI.WebControls;
using Sy***m.Web.UI.WebControls.WebParts;
using Sy***m.Web.UI.HtmlControls;
using Sy***m.Data.SqlClient;
namespace Mysqlserver
{
    ///


    /// SqlServerDataBase 的摘要说明
    ///

    public class SqlServerDataBase
    {
        private string strError = null;
        private int intCount = 0;
        public SqlServerDataBase()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        ///
        /// 公开方法DBConn,返回数据库连接
        ///

        ///
        public SqlConnection DBconn()
        {
            string strConn = "Server=(local);Database=GlobalMeetings;Uid=sa;pwd=";
            try
            {
                return new SqlConnection(strConn);
            }
            catch (Exception)
            {
                return null;
            }
        }
        ///
        /// 公开属性ErrorMessage,返回错误信息
        ///

        public string ErrorMessage
        {
            get
            {
                return strError;
            }
        }

        ///


        /// 根据查询语句从数据库检索数据
        ///

        /// 查询语句
        /// 数据库连接
        /// 有数据则返回DataSet对象,否则返回null
        public DataSet Select(string SelectString, SqlConnection sqlConn)
        {
            strError = "";
            SqlConnection conn;
            if (sqlConn == null)
            {
                conn = DBconn();
            }
            else
            {
                conn = sqlConn;
            }
            try
            {
                //若数据库连接的当前状态是关闭的,则打开连接
                if (co***State == Co***ctionState.Closed)
                {
                    co***Open();
                }
                SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
                SqlCommand selectCommand = new SqlCommand(SelectString, conn);
                se***tCommand.CommandType = Co***ndType.Text;
                my***DataAdapter.SelectCommand = selectCommand;
                DataSet myDS = new DataSet();
                my***DataAdapter.Fill(myDS);
                return myDS;
            }
            catch (Exception e)
            {
                strError = "数据检索失败:" + e.***sage;
                return null;
            }
            finally
            {
                if (co***State != Co***ctionState.Closed)
                {
                    co***Close();
                }
            }
        }
        ///
        /// 更新数据库
        ///

        /// Update Sql语句
        /// 数据库连接
        /// 更新成功返回true
        public bool Update(string UpdateString, SqlConnection SqlConn)
        {
            return udiDataBase(UpdateString, SqlConn);
        }
        ///
        /// 从数据库中删除数据
        ///

        /// Delete Sql语句
        /// 数据库连接
        /// 删除成功返回true
        public bool Delete(string DeleteString, SqlConnection SqlConn)
        {
            return udiDataBase(DeleteString, SqlConn);
        }
        ///
        /// 把数据插入数据库
        ///

        /// Insert Sql语句
        /// 数据库连接
        /// 插入成功返回true
        public bool Insert(string InsertString, SqlConnection SqlConn)
        {
            return udiDataBase(InsertString, SqlConn);
        }
        ///
        /// 根据Sql语句更新数据库
        ///

        /// 更新语句
        /// 数据库连接
        /// 更新成功则返回true
        public bool udiDataBase(string UDIString, SqlConnection SqlConn)
        {
            strError = "";
            SqlConnection conn;
            if (SqlConn == null)
            {
                conn = DBconn();
            }
            else
            {
                conn = SqlConn;
            }
            try
            {
                if (co***State == Co***ctionState.Closed)
                {
                    co***Open();
                }
                SqlCommand cmd = new SqlCommand(UDIString, conn);
                cm***ommandType = Co***ndType.Text;
                intCount = cm***xecuteNonQuery();
                return !(intCount < 1);
            }
            catch (Exception e)
            {
                strError = "更新数据库失败:" + e.***sage;
                return false;
            }
            finally
            {
                if (co***State != Co***ctionState.Closed)
                {
                    co***Close();
                }
            }
        }
    }
}
 -----------------------------
两种调用方法
1、                    string strUserPsw = Us***sw.Text.Trim();
                    string UserPassword = Sy***m.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strUserPsw, "MD5");//md5加密
                    SqlServerDataBase obj = new SqlServerDataBase();
                    ob***nsert("insert into asUserInfo (UserName,UserPassword,Question,Answer,CreateTime) values('" + Us***ame.Text.Trim() + "','" + UserPassword + "','" + Qu***ion.Text.Trim() + "','" + An***r.Text.Trim() + "','" + Da***ime.Now.ToString() + "' )", null);
2、        private bool IsUsernameExist(string strUsername)
        {
            bool bRet = true;
            SqlServerDataBase db = new SqlServerDataBase();
            DataSet ds = db***lect("select * from asUserInfo where UserName = '" + strUsername + "'", null);
            if (ds == null || ds***bles.Count == 0 || ds***bles[0].R***.Count == 0)
            {
                bRet = false;
            }
            else
            {
                bRet = true;
            }

            return bRet;
        }

http://blog.csdn.net/zdyguilong/archive/2007/01/22/1490250.aspx

相关文章 最新文章

相关应用

热门文章

猜你喜欢

返回顶部