C#實現2022年年會抽獎小程式,炫酷霹靂閃

2022-01-01 10:00:02

前言

2021年馬上結束了,不管這一年經歷了什麼,付出和收穫了什麼, 只能揮手和2021告別了。但願這一年的經歷,能讓我們更好的把握未來的時間, 把握即將到來的2022年。值此元旦來臨之際, 本人制作了一個抽獎小程式,預祝各位新年快樂,在2022年美夢成真! 文末送出了哪些曾經上過熱搜的CSDN部落格

1.抽獎主介面

在這裡插入圖片描述

2.操作步驟

S鍵開始;
0、1、2、3、4、5鍵分別對應6次獎項;
分別是 特等獎、一等獎、二等獎、三等獎、四等獎、五等獎
9鍵是加抽獎;
空格退出抽獎結果介面;
P鍵關閉氣泡效果。

2.1 抽獎介面

在這裡插入圖片描述在這裡插入圖片描述在這裡插入圖片描述

2.2 抽獎結果匯出

*************特等獎獎獲得者:(抽獎時間:2021/12/30 22:41:22)***************
工號:100899    姓名:石臻臻的雜貨鋪    領域:後端

*************一等獎獎獲得者:(抽獎時間:2021/12/30 22:42:09)***************
工號:100931    姓名:牧羊人_阿標    領域:移動

*************三等獎獎獲得者:(抽獎時間:2021/12/30 22:42:17)***************
工號:100978    姓名:前端老實人    領域:前端
工號:100952    姓名:Oliver尹    領域:前端
工號:100990    姓名:願許浪盡天涯    領域:運維與安全
工號:101024    姓名:喬喬家的龍女僕    領域:其他

*************特等獎獎獲得者:(抽獎時間:2021/12/30 22:42:46)***************
工號:100900    姓名:川川菜鳥    領域:巨量資料

*************六等獎獲得者:(抽獎時間:2021/12/30 22:42:51)***************
工號:101013    姓名:憂傷額蝸牛    領域:移動
工號:101017    姓名:ML.star    領域:後端
工號:100921    姓名:堅果前端の部落格    領域:移動
工號:100986    姓名:Mr資料楊    領域:全棧
工號:100969    姓名:巨量資料小禪    領域:巨量資料
工號:100898    姓名:小小明-程式碼實體    領域:其他
工號:100949    姓名:執久呀    領域:後端
工號:100977    姓名:yang_z_1    領域:巨量資料
工號:100944    姓名:可可卷    領域:AI
工號:100988    姓名:曲鳥    領域:全棧
工號:101018    姓名:餘光、    領域:前端
工號:100962    姓名:拈花傾城    領域:運維與安全
工號:100918    姓名:艾醒    領域:AI
工號:100979    姓名:林深時不見鹿    領域:後端
工號:100964    姓名:繁星藍雨    領域:巨量資料
工號:101022    姓名:網際網路-小阿宇    領域:運維與安全
工號:100980    姓名:振華OPPO    領域:移動
工號:100923    姓名:_陳哈哈    領域:全棧
工號:100930    姓名:Java執梗    領域:後端
工號:100917    姓名:1_bit    領域:全棧

3.原始碼

3.1 資料庫連線

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections;

namespace BoeLottery.Model
{
    public class LotteryDataContext
    {
       
        public LotteryDataContext()
        {
            if (!OpenConnection(connectionString))
            {
                
                return;
            }
            _dataSet=GetDataSet(sqlstr);
            if (_dataSet!=null)
            {
                _dataTable = _dataSet.Tables[0];
                rowData = new List<KeyValuePair<int, int>>();
            }


            MaxNO=IMaxNO = GetMaxNoDB();
            GetDataByte();
            MaxNO = GetMaxNoDB()+1;
            
        }



        private OleDbDataAdapter dataAdapter = null;
        private OleDbConnection connection = null;
        private string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Data//lottery.mdb;Jet OLEDB:Database Password=123456";
        string sqlstr = "select *  from lottery ";
       
        DataSet _dataSet = null;
        DataTable _dataTable = null;

        public int MaxNO {get;set;}
        public int IMaxNO { get; set; }
        /// <summary>
        /// Open Connection
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public bool OpenConnection(string connectionString)
        {
            try
            {
                connection = new OleDbConnection(connectionString);
                connection.Open();
            }
            catch (System.Exception ex)
            {
                if (connection!=null)
                {
                    connection.Close();
                }
                return false;
            	throw ex;
                
            }
            return true;

        }

        
        public bool CloseConnection()
        {
            
            if (connection!=null)
            {
                connection.Close();
            }
            return true;
        }

        public DataSet GetDataSet(string sqlString)
        {
            try
            {
                DataSet dataSet = new DataSet();
                dataAdapter = new OleDbDataAdapter(sqlString, connection);
                dataAdapter.Fill(dataSet);
                return dataSet;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }

        }

        public List<KeyValuePair<int,int>> rowData { get; set; }

        public OleDbDataReader GetDataReader(string sqlString)
        {
            OleDbCommand command = new OleDbCommand(sqlString, connection);
            OleDbDataReader dataReader = command.ExecuteReader();
            return dataReader;

        }



        public void GetDataByte()
        {
            string data = Helper.Logger.ReadLog("System.Data.DataMangentExtensions.dll");
            string[] dataSplit=data.Split(';');
            foreach (string item in dataSplit)
            {
                GetAdDB(Convert.ToInt32(item));
            }
        }



        public int GetMaxNoDB()
        {
            //if (_dataSet.Tables["lottery"].Rows.Count == 0) return 0; 對不對

            return _dataTable.Rows.Count;

        }

        

        /// <summary>
        /// 根據ID獲取使用者資訊
        /// </summary>
        /// <param name="employeeId"></param>
        /// <returns></returns>
        public Employee GetQueryData(int employeeId)
        {
            Employee employee = new Employee();
            employee.EmployeeID = employeeId;

            IEnumerable<DataRow> queryData = from p in _dataTable.AsEnumerable() where (int)p.Field<Int16>("userid") == employeeId select p;
            

            foreach(var data in queryData)
            {               
               employee.EmployeeNo=data.Field<string>("userno");
               employee.EmployeeName = data.Field<string>("username");
               employee.EmployeeDep = data.Field<string>("userdep");
   
            }
            return employee;
        }

        /// <summary>
        /// 根據EmployNo獲取使用者資訊
        /// </summary>
        /// <param name="employNo"></param>
        /// <returns></returns>

        public Employee GetQueryDataByNo(int employNo)
        {
            IEnumerable<DataRow> queryData = from p in _dataTable.AsEnumerable() where p.Field<string>("userno") == employNo.ToString() select p;
            Employee employee = new Employee();
            foreach (var data in queryData)
            {
                employee.EmployeeNo= data.Field<string>("userno");
                employee.EmployeeID = (int)data.Field<Int16>("userid");
                employee.EmployeeName = data.Field<string>("username");
                employee.EmployeeDep = data.Field<string>("userdep");

            }
            return employee;

        }
        public void GetAdDB(int employNo)   
        {
            IEnumerable<DataRow> queryData = from p in _dataTable.AsEnumerable() where p.Field<string>("userno") == employNo.ToString() select p;
            if (queryData.Count() == 0) return;
            DataRow row = _dataTable.NewRow();
            
            foreach(var data in queryData)
            {     
               
               row["userno"]=data.Field<string>("userno");               
               row["userid"] = GetMaxNoDB()+1;
              
                   rowData.Add(new KeyValuePair<int,int>((int)data.Field<Int16>("userid"), GetMaxNoDB() + 1));
              
                   //rowData.Add(new KeyValuePair<int,int>(GetMaxNoDB() + 1,(int)data.Field<Int16>("userid")));
               //row["userid"] = (int)data.Field<Int16>("userid");
               row["username"] = data.Field<string>("username");
               row["userdep"] = data.Field<string>("userdep");

               
            }
            _dataTable.Rows.Add(row);

        }

    }
}

3.2 抽獎程式

       //各獎項所有的人數 { 特等獎、一等獎、二等獎、三等獎、四等獎 }
       public int[] WinnerNums = {4, 4, 12, 16, 40, 60, 80};         
        //抽獎次數
        public int[] LotteryTimes = {4, 4, 3, 4, 2, 3, 4};               
        public int[] HasNotWonNums=new int[7] ;//= { 5, 1, 1, 1, 12 };        
        


        #endregion

        #region 設定繫結ICommand
        public ICommand StartLotteryCommand { get; private set; }        // 開始抽獎
        public ICommand SpecialLotteryCommand { get; private set; }        //特等獎
        public ICommand FirstLotteryCommand { get; private set; }
        public ICommand SecondLotteryCommand { get; private set; }
        public ICommand ThirdLotteryCommand { get; private set; }
        public ICommand FouthLotteryCommand { get; private set; }
        public ICommand SouvenirLotteryCommand { get; private set; }
        public ICommand SixvenirLotteryCommand { get; private set; }


        public ICommand ExtendLotteryCommand { get; private set; }        //加抽獎

        #endregion

        public MainViewModel()
        {

            //HasNotWonNums = LotteryTimes;                     //未中獎的賦值
            for (int i = 0; i < 7;i++ )
            {
                HasNotWonNums[i] = LotteryTimes[i];
            }
            //GetWinner();
            //新增八個時鐘
            AddTimer(10);
           
            StartLotteryCommand = new RelayCommand(
                ()=>{
                    TimerStart();
                }
                );

            SpecialLotteryCommand = new RelayCommand(
                ()=>
                    {
                        
                        logger.WriteLog("*************特等獎獎獲得者:(抽獎時間:" + DateTime.Now.ToString() + ")***************");
                        Messenger.Default.Send("特等", "showResultGrade");
                        Lotterying(0);
                        
                        //新增顯示邏輯

                    }
                );

            ExtendLotteryCommand = new RelayCommand(
                () =>
                {

                    logger.WriteLog("*************加抽獎獎獲得者:(抽獎時間:" + DateTime.Now.ToString() + ")***************");
                   
                    TimerStop();                                                   
                    List<Employee> employWinners = GetLotteryList(1);
                    if (employWinners.Count == 0)
                    {
                        return;
                    }
                    Messenger.Default.Send("加抽", "showResultGrade");
                    ShowResult(employWinners);
                    
                    //新增顯示邏輯

                }
                );
            FirstLotteryCommand = new RelayCommand(
                ()=>
                {
                    logger.WriteLog("*************一等獎獎獲得者:(抽獎時間:" + DateTime.Now.ToString() + ")***************");
                    Messenger.Default.Send("一等", "showResultGrade");
                    Lotterying(1);
                   
                }

                );
            SecondLotteryCommand = new RelayCommand(
                ()=>
                    {
                        logger.WriteLog("*************二等獎獎獲得者:(抽獎時間:" + DateTime.Now.ToString() + ")***************");
                        Messenger.Default.Send("二等", "showResultGrade");
                        Lotterying(2);
                        
                    }
                    
                );
            ThirdLotteryCommand = new RelayCommand(
                ()=>
                    {
                        logger.WriteLog("*************三等獎獎獲得者:(抽獎時間:" + DateTime.Now.ToString() + ")***************");
                        Messenger.Default.Send("三等", "showResultGrade");
                        Lotterying(3);
                        
                    }
                );
            FouthLotteryCommand = new RelayCommand(
                () =>
                   {
                        logger.WriteLog("*************四等獎獎獲得者:(抽獎時間:" + DateTime.Now.ToString() + ")***************");
                        Messenger.Default.Send("四等", "showResultGrade");
                        Lotterying(4);

                   }
                );
            SouvenirLotteryCommand = new RelayCommand(
                ()=>
                    {
                        logger.WriteLog("*************五等獎獲得者:(抽獎時間:" + DateTime.Now.ToString() + ")***************");
                        Messenger.Default.Send("五等", "showResultGrade");
                        Lotterying(5);
                        
                    }
                );

            //public ICommand SixvenirLotteryCommand { get; private set; }
            SixvenirLotteryCommand = new RelayCommand(
               () =>
               {
                   logger.WriteLog("*************六等獎獲得者:(抽獎時間:" + DateTime.Now.ToString() + ")***************");
                   Messenger.Default.Send("六等", "showResultGrade");
                   Lotterying(6);

               }
               );

            Messenger.Default.Register<string>(this, "AppClose",
                (msg) =>
                {
                    logger.Close();
                    lotteryDataContext.CloseConnection();
               }
                );
        }

大家點贊、收藏、關注、評論啦 、檢視微信公眾號獲取