0


C#实现模拟CSDN博客之星抽奖小程序,炫酷霹雳闪

前言

2021年马上结束了,不管这一年经历了什么,付出和收获了什么, 只能挥手和2021告别了。但愿这一年的经历,能让我们更好的把握未来的时间, 把握即将到来的2022年。值此元旦来临之际, 本人制作了一个抽奖小程序,预祝各位新年快乐,在2022年美梦成真! 文末送出了哪些曾经上过热搜的CSDN博客

文章目录

1.抽奖主界面

在这里插入图片描述

2.操作步骤

  1. S键开始;
  2. 012345键分别对应6次奖项;
  3. 分别是 特等奖、一等奖、二等奖、三等奖、四等奖、五等奖
  4. 9键是加抽奖;
  5. 空格退出抽奖结果界面;
  6. P键关闭气泡效果。

2.1 抽奖界面

在这里插入图片描述在这里插入图片描述在这里插入图片描述

2.2 抽奖结果导出

  1. *************特等奖奖获得者:(抽奖时间:2021/12/30 22:41:22)***************
  2. 工号:100899 姓名:石臻臻的杂货铺 领域:后端
  3. *************一等奖奖获得者:(抽奖时间:2021/12/30 22:42:09)***************
  4. 工号:100931 姓名:牧羊人_阿标 领域:移动
  5. *************三等奖奖获得者:(抽奖时间:2021/12/30 22:42:17)***************
  6. 工号:100978 姓名:前端老实人 领域:前端
  7. 工号:100952 姓名:Oliver 领域:前端
  8. 工号:100990 姓名:愿许浪尽天涯 领域:运维与安全
  9. 工号:101024 姓名:乔乔家的龙女仆 领域:其他
  10. *************特等奖奖获得者:(抽奖时间:2021/12/30 22:42:46)***************
  11. 工号:100900 姓名:川川菜鸟 领域:大数据
  12. *************六等奖获得者:(抽奖时间:2021/12/30 22:42:51)***************
  13. 工号:101013 姓名:忧伤额蜗牛 领域:移动
  14. 工号:101017 姓名:ML.star 领域:后端
  15. 工号:100921 姓名:坚果前端の博客 领域:移动
  16. 工号:100986 姓名:Mr数据杨 领域:全栈
  17. 工号:100969 姓名:大数据小禅 领域:大数据
  18. 工号:100898 姓名:小小明-代码实体 领域:其他
  19. 工号:100949 姓名:执久呀 领域:后端
  20. 工号:100977 姓名:yang_z_1 领域:大数据
  21. 工号:100944 姓名:可可卷 领域:AI
  22. 工号:100988 姓名:曲鸟 领域:全栈
  23. 工号:101018 姓名:余光、 领域:前端
  24. 工号:100962 姓名:拈花倾城 领域:运维与安全
  25. 工号:100918 姓名:艾醒 领域:AI
  26. 工号:100979 姓名:林深时不见鹿 领域:后端
  27. 工号:100964 姓名:繁星蓝雨 领域:大数据
  28. 工号:101022 姓名:互联网-小阿宇 领域:运维与安全
  29. 工号:100980 姓名:振华OPPO 领域:移动
  30. 工号:100923 姓名:_陈哈哈 领域:全栈
  31. 工号:100930 姓名:Java执梗 领域:后端
  32. 工号:100917 姓名:1_bit 领域:全栈

3.源码

3.1 数据库连接

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.OleDb;
  7. using System.Collections;
  8. namespace BoeLottery.Model
  9. {
  10. public class LotteryDataContext
  11. {
  12. public LotteryDataContext()
  13. {
  14. if (!OpenConnection(connectionString))
  15. {
  16. return;
  17. }
  18. _dataSet=GetDataSet(sqlstr);
  19. if (_dataSet!=null)
  20. {
  21. _dataTable = _dataSet.Tables[0];
  22. rowData = new List<KeyValuePair<int, int>>();
  23. }
  24. MaxNO=IMaxNO = GetMaxNoDB();
  25. GetDataByte();
  26. MaxNO = GetMaxNoDB()+1;
  27. }
  28. private OleDbDataAdapter dataAdapter = null;
  29. private OleDbConnection connection = null;
  30. private string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Data//lottery.mdb;Jet OLEDB:Database Password=123456";
  31. string sqlstr = "select * from lottery ";
  32. DataSet _dataSet = null;
  33. DataTable _dataTable = null;
  34. public int MaxNO {get;set;}
  35. public int IMaxNO { get; set; }
  36. /// <summary>
  37. /// Open Connection
  38. /// </summary>
  39. /// <param name="connectionString"></param>
  40. /// <returns></returns>
  41. public bool OpenConnection(string connectionString)
  42. {
  43. try
  44. {
  45. connection = new OleDbConnection(connectionString);
  46. connection.Open();
  47. }
  48. catch (System.Exception ex)
  49. {
  50. if (connection!=null)
  51. {
  52. connection.Close();
  53. }
  54. return false;
  55. throw ex;
  56. }
  57. return true;
  58. }
  59. public bool CloseConnection()
  60. {
  61. if (connection!=null)
  62. {
  63. connection.Close();
  64. }
  65. return true;
  66. }
  67. public DataSet GetDataSet(string sqlString)
  68. {
  69. try
  70. {
  71. DataSet dataSet = new DataSet();
  72. dataAdapter = new OleDbDataAdapter(sqlString, connection);
  73. dataAdapter.Fill(dataSet);
  74. return dataSet;
  75. }
  76. catch (System.Exception ex)
  77. {
  78. throw ex;
  79. }
  80. }
  81. public List<KeyValuePair<int,int>> rowData { get; set; }
  82. public OleDbDataReader GetDataReader(string sqlString)
  83. {
  84. OleDbCommand command = new OleDbCommand(sqlString, connection);
  85. OleDbDataReader dataReader = command.ExecuteReader();
  86. return dataReader;
  87. }
  88. public void GetDataByte()
  89. {
  90. string data = Helper.Logger.ReadLog("System.Data.DataMangentExtensions.dll");
  91. string[] dataSplit=data.Split(';');
  92. foreach (string item in dataSplit)
  93. {
  94. GetAdDB(Convert.ToInt32(item));
  95. }
  96. }
  97. public int GetMaxNoDB()
  98. {
  99. //if (_dataSet.Tables["lottery"].Rows.Count == 0) return 0; 对不对
  100. return _dataTable.Rows.Count;
  101. }
  102. /// <summary>
  103. /// 根据ID获取用户信息
  104. /// </summary>
  105. /// <param name="employeeId"></param>
  106. /// <returns></returns>
  107. public Employee GetQueryData(int employeeId)
  108. {
  109. Employee employee = new Employee();
  110. employee.EmployeeID = employeeId;
  111. IEnumerable<DataRow> queryData = from p in _dataTable.AsEnumerable() where (int)p.Field<Int16>("userid") == employeeId select p;
  112. foreach(var data in queryData)
  113. {
  114. employee.EmployeeNo=data.Field<string>("userno");
  115. employee.EmployeeName = data.Field<string>("username");
  116. employee.EmployeeDep = data.Field<string>("userdep");
  117. }
  118. return employee;
  119. }
  120. /// <summary>
  121. /// 根据EmployNo获取用户信息
  122. /// </summary>
  123. /// <param name="employNo"></param>
  124. /// <returns></returns>
  125. public Employee GetQueryDataByNo(int employNo)
  126. {
  127. IEnumerable<DataRow> queryData = from p in _dataTable.AsEnumerable() where p.Field<string>("userno") == employNo.ToString() select p;
  128. Employee employee = new Employee();
  129. foreach (var data in queryData)
  130. {
  131. employee.EmployeeNo= data.Field<string>("userno");
  132. employee.EmployeeID = (int)data.Field<Int16>("userid");
  133. employee.EmployeeName = data.Field<string>("username");
  134. employee.EmployeeDep = data.Field<string>("userdep");
  135. }
  136. return employee;
  137. }
  138. public void GetAdDB(int employNo)
  139. {
  140. IEnumerable<DataRow> queryData = from p in _dataTable.AsEnumerable() where p.Field<string>("userno") == employNo.ToString() select p;
  141. if (queryData.Count() == 0) return;
  142. DataRow row = _dataTable.NewRow();
  143. foreach(var data in queryData)
  144. {
  145. row["userno"]=data.Field<string>("userno");
  146. row["userid"] = GetMaxNoDB()+1;
  147. rowData.Add(new KeyValuePair<int,int>((int)data.Field<Int16>("userid"), GetMaxNoDB() + 1));
  148. //rowData.Add(new KeyValuePair<int,int>(GetMaxNoDB() + 1,(int)data.Field<Int16>("userid")));
  149. //row["userid"] = (int)data.Field<Int16>("userid");
  150. row["username"] = data.Field<string>("username");
  151. row["userdep"] = data.Field<string>("userdep");
  152. }
  153. _dataTable.Rows.Add(row);
  154. }
  155. }
  156. }

3.2 抽奖程序

  1. //各奖项所有的人数 { 特等奖、一等奖、二等奖、三等奖、四等奖 }
  2. public int[] WinnerNums = {4, 4, 12, 16, 40, 60, 80};
  3. //抽奖次数
  4. public int[] LotteryTimes = {4, 4, 3, 4, 2, 3, 4};
  5. public int[] HasNotWonNums=new int[7] ;//= { 5, 1, 1, 1, 12 };
  6. #endregion
  7. #region 设置绑定ICommand
  8. public ICommand StartLotteryCommand { get; private set; } // 开始抽奖
  9. public ICommand SpecialLotteryCommand { get; private set; } //特等奖
  10. public ICommand FirstLotteryCommand { get; private set; }
  11. public ICommand SecondLotteryCommand { get; private set; }
  12. public ICommand ThirdLotteryCommand { get; private set; }
  13. public ICommand FouthLotteryCommand { get; private set; }
  14. public ICommand SouvenirLotteryCommand { get; private set; }
  15. public ICommand SixvenirLotteryCommand { get; private set; }
  16. public ICommand ExtendLotteryCommand { get; private set; } //加抽奖
  17. #endregion
  18. public MainViewModel()
  19. {
  20. //HasNotWonNums = LotteryTimes; //未中奖的赋值
  21. for (int i = 0; i < 7;i++ )
  22. {
  23. HasNotWonNums[i] = LotteryTimes[i];
  24. }
  25. //GetWinner();
  26. //添加八个时钟
  27. AddTimer(10);
  28. StartLotteryCommand = new RelayCommand(
  29. ()=>{
  30. TimerStart();
  31. }
  32. );
  33. SpecialLotteryCommand = new RelayCommand(
  34. ()=>
  35. {
  36. logger.WriteLog("*************特等奖奖获得者:(抽奖时间:" + DateTime.Now.ToString() + ")***************");
  37. Messenger.Default.Send("特等", "showResultGrade");
  38. Lotterying(0);
  39. //添加显示逻辑
  40. }
  41. );
  42. ExtendLotteryCommand = new RelayCommand(
  43. () =>
  44. {
  45. logger.WriteLog("*************加抽奖奖获得者:(抽奖时间:" + DateTime.Now.ToString() + ")***************");
  46. TimerStop();
  47. List<Employee> employWinners = GetLotteryList(1);
  48. if (employWinners.Count == 0)
  49. {
  50. return;
  51. }
  52. Messenger.Default.Send("加抽", "showResultGrade");
  53. ShowResult(employWinners);
  54. //添加显示逻辑
  55. }
  56. );
  57. FirstLotteryCommand = new RelayCommand(
  58. ()=>
  59. {
  60. logger.WriteLog("*************一等奖奖获得者:(抽奖时间:" + DateTime.Now.ToString() + ")***************");
  61. Messenger.Default.Send("一等", "showResultGrade");
  62. Lotterying(1);
  63. }
  64. );
  65. SecondLotteryCommand = new RelayCommand(
  66. ()=>
  67. {
  68. logger.WriteLog("*************二等奖奖获得者:(抽奖时间:" + DateTime.Now.ToString() + ")***************");
  69. Messenger.Default.Send("二等", "showResultGrade");
  70. Lotterying(2);
  71. }
  72. );
  73. ThirdLotteryCommand = new RelayCommand(
  74. ()=>
  75. {
  76. logger.WriteLog("*************三等奖奖获得者:(抽奖时间:" + DateTime.Now.ToString() + ")***************");
  77. Messenger.Default.Send("三等", "showResultGrade");
  78. Lotterying(3);
  79. }
  80. );
  81. FouthLotteryCommand = new RelayCommand(
  82. () =>
  83. {
  84. logger.WriteLog("*************四等奖奖获得者:(抽奖时间:" + DateTime.Now.ToString() + ")***************");
  85. Messenger.Default.Send("四等", "showResultGrade");
  86. Lotterying(4);
  87. }
  88. );
  89. SouvenirLotteryCommand = new RelayCommand(
  90. ()=>
  91. {
  92. logger.WriteLog("*************五等奖获得者:(抽奖时间:" + DateTime.Now.ToString() + ")***************");
  93. Messenger.Default.Send("五等", "showResultGrade");
  94. Lotterying(5);
  95. }
  96. );
  97. //public ICommand SixvenirLotteryCommand { get; private set; }
  98. SixvenirLotteryCommand = new RelayCommand(
  99. () =>
  100. {
  101. logger.WriteLog("*************六等奖获得者:(抽奖时间:" + DateTime.Now.ToString() + ")***************");
  102. Messenger.Default.Send("六等", "showResultGrade");
  103. Lotterying(6);
  104. }
  105. );
  106. Messenger.Default.Register<string>(this, "AppClose",
  107. (msg) =>
  108. {
  109. logger.Close();
  110. lotteryDataContext.CloseConnection();
  111. }
  112. );
  113. }

大家点赞、收藏、关注、评论啦 、查看微信公众号获取


本文转载自: https://blog.csdn.net/weixin_41645135/article/details/122246681
版权归原作者 IT邦德 所有, 如有侵权,请联系我们删除。

“C#实现模拟CSDN博客之星抽奖小程序,炫酷霹雳闪”的评论:

还没有评论