亲宝软件园·资讯

展开

C#抽奖小程序

IT邦德 人气:0

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

加载全部内容

相关教程
猜你喜欢
用户评论