⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 roomdbfunc.cs

📁 JAVA的精彩实例
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.OleDb;
using feiyun0112.cnblogs.com.CSDNReader.Model;

namespace feiyun0112.cnblogs.com.CSDNReader.Functions
{
    class RoomDBFunc
    {
        private RoomDBFunc()
        {
        }
        public static bool AddRoom(Room r)
        {
            DBFunc db = new DBFunc(false, false);
            try
            {
                int intMaxLevel = db.GetIntValue("select max(RoomLevel) from RoomInfo")+1;
                int intMaxID = db.GetIntValue("select max(RoomID) from RoomInfo")+1;
                string strSql = string.Format("insert into RoomInfo(RoomID,RoomLevel,RoomName,RoomURL,isGroup,GroupID) values({0},{1},'{2}','{3}',{4},{5})"
                        , intMaxID, intMaxLevel , DBFunc.SpecialChar(r.Name), DBFunc.SpecialChar(r.URL),r.IsGroup,r.GroupID
                    );
                db.ExecuteSql(strSql);

                

                db.CloseCon();

                r.ID = intMaxID;
            }
            catch (Exception e)
            {
                MsgFunc.ShowWarning(e.Message);
                return false ;
            }
            finally
            {
                db.Dispose();
            }
            return true;
        }

        public static bool EditRoom(Room oldr,Room r)
        {
            DBFunc db = new DBFunc(false, false);
            try
            {

                string strSql = string.Format("update RoomInfo set RoomName='{0}',RoomURL='{1}',isGroup={2},GroupID={3} where RoomID={4}"
                        , DBFunc.SpecialChar(r.Name), DBFunc.SpecialChar(r.URL), r.IsGroup, r.GroupID, oldr.ID
                    );
                db.ExecuteSql(strSql);

                if (!( oldr.GroupID == r.GroupID))
                {
                    strSql=string.Format("update RoomInfo set RoomLevel={0} where RoomID={1}",db.GetIntValue("select max(RoomLevel) from RoomInfo")+1, oldr.ID);
                    db.ExecuteSql(strSql);
                }

                db.CloseCon();
            }
            catch (Exception e)
            {
                MsgFunc.ShowWarning(e.Message);
                return false;
            }
            finally
            {
                db.Dispose();
            }
            return true;
        }

        public static bool DelRoom(Room r)
        {
            DBFunc db = new DBFunc(false, false);
            try
            {
                string strSql;

                strSql = string.Format("delete * from RoomInfo  where RoomID={0}"
                        , r.ID
                    );
                db.ExecuteSql(strSql);

                 DelChildRoom(r.ID , db);

                db.CloseCon();
            }
            catch (Exception e)
            {
                MsgFunc.ShowWarning(e.Message);
                return false;
            }
            finally
            {
                db.Dispose();
            }
            return true;
        }

        private static void  DelChildRoom(int intGroupID, DBFunc db)
        {
            string strSql;
            DataSet ds = db.GetDataSet(string.Format("select RoomID,RoomName,RoomURL,IsGroup from RoomInfo where  GroupID={0} order by RoomLevel", intGroupID));
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count  > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    DelChildRoom(int.Parse( dr[0].ToString()), db);
                }
            }
            strSql = string.Format("delete * from RoomInfo  where GroupID>0 and GroupID={0}"
                            , intGroupID
                        );
            db.ExecuteSql(strSql);

        }


        public static IList<Room> GetRooms()
        {
            IList<Room> Rooms = new List<Room>();
            DBFunc db = new DBFunc(false, false);
            OleDbDataReader reader = db.GetDataReader("select RoomID,RoomName,RoomURL,IsGroup,GroupID from RoomInfo where (GroupID=0 or groupid is null) order by RoomLevel");
            while (reader.Read())
            {
                Room r = new Room();
                r.ID = reader.GetInt32(0);
                r.Name = reader.GetString(1);                
                r.IsGroup = reader.GetBoolean(3);
                if (!r.IsGroup)
                {
                    r.URL = reader.GetString(2);
                }
                r.GroupID = 0;
                Rooms.Add(r);
            }
            reader.Close();
            db.CloseCon();

            return Rooms;
        }

        public static IList<Room> GetChildRooms(int intGroupID)
        {
            IList<Room> Rooms = new List<Room>();
            DBFunc db = new DBFunc(false, false);
            OleDbDataReader reader = db.GetDataReader(string.Format("select RoomID,RoomName,RoomURL,IsGroup from RoomInfo where  GroupID={0} order by RoomLevel", intGroupID));
            while (reader.Read())
            {
                Room r = new Room();
                r.ID = reader.GetInt32(0);
                r.Name = reader.GetString(1);
                r.URL = reader.GetString(2);
                r.IsGroup = reader.GetBoolean(3);
                r.GroupID = intGroupID;
                Rooms.Add(r);
            }
            reader.Close();
            db.CloseCon();

            return Rooms;
        }

        public static IList<Room> GetGroupRooms()
        {
            IList<Room> Rooms = new List<Room>();
            DBFunc db = new DBFunc(false, false);
            OleDbDataReader reader = db.GetDataReader("select RoomID,RoomName,RoomURL,IsGroup from RoomInfo where isGroup<>0 order by GroupID,RoomLevel");
            while (reader.Read())
            {
                Room r = new Room();
                r.ID = reader.GetInt32(0);
                r.Name = reader.GetString(1);                
                Rooms.Add(r);
            }
            reader.Close();
            db.CloseCon();

            return Rooms;
        }


        /*
        public  bool  CheckURL(string strURL,int intID)
        {
            DBFunc db = new DBFunc(false, false);
            try
            {

                string strSql = string.Format("select * from RoomInfo  where  RoomURL='{0}' and RoomID<>{1}"
                        ,strURL,intID
                    );
                if (db.GetDataFirstRow(strSql) != null)
                {
                    MsgFunc.ShowWarning("URL 已经存在!");
                    return false;
                }
                db.CloseCon();
            }
            catch (Exception e)
            {
                MsgFunc.ShowWarning(e.Message);
                return false;
            }
            finally
            {
                db.CloseCon();
            }
            return true;
        }
        */
        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -