欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#模拟实现抽奖小程序的示例代码

程序员文章站 2022-06-24 18:29:29
目录1.抽奖主界面2.操作步骤2.1 抽奖界面2.2 抽奖结果导出3.源码3.1 数据库连接3.2 抽奖程序1.抽奖主界面2.操作步骤s键开始;0、1、2、3、4、5键分别对应6次奖项;分别是 特等奖...

1.抽奖主界面

C#模拟实现抽奖小程序的示例代码

2.操作步骤

s键开始;

0、1、2、3、4、5键分别对应6次奖项;

分别是 特等奖、一等奖、二等奖、三等奖、四等奖、五等奖

9键是加抽奖;

空格退出抽奖结果界面;

p键关闭气泡效果。

2.1 抽奖界面

C#模拟实现抽奖小程序的示例代码

C#模拟实现抽奖小程序的示例代码

C#模拟实现抽奖小程序的示例代码

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#抽奖小程序的资料请关注其它相关文章!

相关标签: C# 抽奖