论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > asp教程
Tag:入门,文摘,实例,技巧,iis,表单,对象,上传,数据库,记录集,session,cookies,存储过程,注入,分页,安全,优化,xmlhttp,fso,jmail,application,防盗链,stream,组件,md5,乱码,缓存,加密,验证码,算法,ubb,正则表达式,水印,,日志,压缩,url重写,控件,函数,破解,触发器,socket,ADO,初学,聊天室,留言本,视频教程

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

文章类别:asp | 发表日期:2008-10-5 20:46:30

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

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

视频教程列表
文章教程搜索
 
Asp推荐教程
Asp热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058