C#模擬實現抽獎小程序的示例代碼

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

以上就是C#模擬實現抽獎小程序的示例代碼的詳細內容,更多關於C#抽獎小程序的資料請關註WalkonNet其它相關文章!

推薦閱讀: