蓝派网(www.lan27.com)-精选网络资源,分享和交流! 文章首页站内搜索在线手册广告代码酷站欣赏万年历
您现在的位置: 蓝派网 >> 文章中心 >> 网络编程 >> .NET >> ASP.NET >> 正文

.net 2005 类 事例

作者:佚名    文章来源:网络    更新时间:2008-10-29 19:05:12

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 ado.Sql
{
    /// <summary>
    /// 本类是方便对新闻系统的数据库进行操作,为了使用时方便,特写了这样一个类,以方便大家慢慢养成面向对象编程的习惯。
    /// </summary>
    public class AdoSql    
    {
        public AdoSql()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        ///  执行SQL语句-读
        /// </summary>
        /// <param name="SqlString">SQL语句</param>
        /// <returns></returns>
        public DataTable ExecuteSqlRead(string SqlString)    
        {
            SqlCommand SqlCmd = new SqlCommand();
            SqlCmd.Connection = new SqlConnection(ConfigurationSettings.AppSettings["ConnString"]);
            SqlCmd.CommandText = SqlString;
            SqlCmd.CommandType = CommandType.Text;
            SqlDataAdapter SqlAd = new SqlDataAdapter(SqlCmd);
            DataSet Rs = new DataSet();
            SqlAd.Fill(Rs);
            return Rs.Tables[0];
        }
        /// <summary>
        /// 执行SQL语句-写
        /// </summary>
        /// <param name="SqlString">SQL语句</param>
        /// <returns></returns>
        public Boolean ExecuteSqlWrite(string SqlString)      
        {
            SqlCommand SqlCmd = new SqlCommand();
            SqlCmd.Connection = new SqlConnection(ConfigurationSettings.AppSettings["ConnString"]);
            SqlCmd.CommandText = SqlString;
            SqlCmd.CommandType = CommandType.Text;
            SqlCmd.Connection.Open();
            SqlCmd.ExecuteNonQuery();
            SqlCmd.Connection.Close();
            return true;
        }
        /// <summary>
        /// 读取新闻系统的数据列表
        /// </summary>
        /// <returns></returns>
        public DataTable ReadAllNews()      
        {
            string SqlString = "select NewsID,NameStr,NewsType,TitleStr,DateTimeTxt,VisitInt from NewsTxt order by NewsID desc";
            return ExecuteSqlRead(SqlString);
        }
        ///<summary>
        ///读ContentTxt
        ///</summary>
        ///<param name="ContentTxt"></param>
        public DataTable ReadContentTxt()
        {
            string SqlString = "select ContentTxt from NewsTet";
            return ExecuteSqlRead(SqlString);
        }
        /// <summary>
        /// 读取新闻分类
        /// </summary>
        /// <returns></returns>
        public DataTable ReadAllType()    
        {
            String SqlString = "select NewsType from NewsType";
            return ExecuteSqlRead(SqlString);
        }

        /// <summary>
        /// 发布新闻
        /// </summary>
        /// <param name="NewsType"></param>
        /// <param name="TitleStr"></param>
        /// <param name="NameStr"></param>
        /// <param name="ContentTxt"></param>
        public void AddNews(string NewsType, string TitleStr, string NameStr, string ContentTxt)      
        {
            int n = 0;      //初始化访问次数为0
            String SqlString = "insert NewsTxt (NameStr,NewsType,TitleStr,ContentTxt,DateTimeTxt,VisitInt) values ('" + NameStr + "','" + NewsType + "','" + TitleStr + "','" + ContentTxt + "','" + DateTime.Now + "','" + n + "')";
            ExecuteSqlWrite(SqlString);
        }
        ///<sumary>
        ///添加用户
        ///</sumary>
        ///<param name="UserNameStr"></param>
        ///<param name="PassWordStr"></param>
        ///<param name="LevelInt"></param>
        public void AddUser(string UserNameStr, string PassWordStr)
        {
            int n = 1;//LevelInt初始值为1
            string SqlString = "insert MUser(UserNameStr,PassWordStr,LevelInt)values('" + UserNameStr + "','" + PassWordStr + "','" + n + "')";
            ExecuteSqlWrite(SqlString);
        }
        ///<sumary>
        ///添加栏目
        ///</sumary>
        ///<param name="NewType"></param>
        public void AddNewType(string NewType)
        {
            string SqlString = "insert NewsType(NewsType)values('" + NewType + "')";
            ExecuteSqlWrite(SqlString);
        }
        /// <summary>
        /// 验证登陆  
        /// </summary>
        /// <param name="UserNameStr"></param>
        /// <param nampe="PassWordStr"></param>
        public static Boolean LoginCheck(string UserNameStr, string PassWordStr)      
        {
            string SqlString = "select UserNameStr,PassWordStr from MUser where UserNameStr='" + UserNameStr + "'and PassWordStr='" + PassWordStr + "'";
            SqlConnection SqlConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnString"]);
            SqlCommand SqlCmd = new SqlCommand(SqlString, SqlConn);
            SqlConn.Open();
            SqlDataReader SqlReader = SqlCmd.ExecuteReader();
            Boolean FindUser = false;
            while (SqlReader.Read())
            {
                FindUser = true;
            }
            if (FindUser)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public DataTable ReadNewsByID(int NewsID)
        {
            string SqlString = "select NameStr,TitleStr,ContentTxt,DateTimeTxt,VisitInt from NewsTxt where NewsID=" + NewsID;
            return ExecuteSqlRead(SqlString);
        }
        public void SetCount(int NewsID)
        {
            string SqlString = "update NewsTxt set VisitInt=VisitInt+1 where NewsId=" + NewsID;
            ExecuteSqlWrite(SqlString);
        }
        public void UpdateNews(string TitleStr, string ContentTxt, int NewsID)
        {
            String SqlString = "Update NewsTxt set TitleStr='" + TitleStr + "',ContentTxt='" + ContentTxt + "' where NewsID=" + NewsID;
            ExecuteSqlWrite(SqlString);
        }

    }
    }

发表评论】【打印此文】【关闭窗口】【点击数:
★好玩的休闲小游戏★