如何对一卡通系统进行优化?

  private void 确认充值_Click(object sender, EventArgs e)
        {
            try
            {
                string sql;
                string connString = @"Server=LAPTOP-8DBAVB04;Database=keke;Trusted_Connection=Yes;";
                SqlConnection myconn = new SqlConnection(connString);
                myconn.Open();
                SqlCommand mycomm = new SqlCommand();
                mycomm.Connection = myconn;
                sql = "insert into Recharge_record(userNo,boadminPwd,rechargeTime) values(@userNo,@boadminPwd,@rechargeTime)";
                mycomm.CommandText = sql;
                mycomm.Parameters.AddRange(
                new SqlParameter[]
                {
                new SqlParameter("@userNo",SqlDbType.VarChar){Value=this.卡No.Text},
                new SqlParameter("@boadminPwd",SqlDbType.Float){Value=this.money.Text},
                new SqlParameter("@rechargeTime",SqlDbType.VarChar){Value=DateTime.Now.ToString("yyyy-MM-dd") },
            });
                int cnt;
                cnt = mycomm.ExecuteNonQuery();
                if (cnt > 0)
                    MessageBox.Show("充值成功!");
                else
                    MessageBox.Show("充值失败!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void 确认查询_Click(object sender, EventArgs e)
        {
            try
            {
                string sql;
                string connString = @"Server=LAPTOP-8DBAVB04;Database=keke;Trusted_Connection=Yes;";
                SqlConnection myconn = new SqlConnection(connString);
                myconn.Open();
                SqlCommand mycomm = new SqlCommand();
                mycomm.Connection = myconn;
                sql = "select * from Recharge_record where userNo like '%@userNo%'";
                sql = sql.Replace("@userNo", 输入框_查询.Text.Trim());
                mycomm.CommandText = sql;
                SqlDataAdapter dr = new SqlDataAdapter(mycomm);
                DataTable table = new DataTable();
                dr.Fill(table);
                this.查询框_充值记录.DataSource = table.DefaultView;
                myconn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void 查询全部_Click(object sender, EventArgs e)
        {
            try
            {
                string sql;
                string connString = @"Server=LAPTOP-8DBAVB04;Database=keke;Trusted_Connection=Yes;";
                SqlConnection myconn = new SqlConnection(connString);
                myconn.Open();
                SqlCommand mycomm = new SqlCommand();
                mycomm.Connection = myconn;
                sql = "select userNo,boadminPwd,rechargeTime from Recharge_record";
                mycomm.CommandText = sql;
                SqlDataAdapter dr = new SqlDataAdapter(mycomm);
                DataTable table = new DataTable();
                dr.Fill(table);
                this.查询框_充值记录.DataSource = table.DefaultView;
                myconn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void 卡No_TextChanged(object sender, EventArgs e)
        {
            int count = 卡No.Text.Length;
            if (count == 8)
            {
                try
                {
                    int.Parse(卡No.Text);
                }
                catch
                {
                    MessageBox.Show("只能输入数字!");
                    卡No.Text = null;
                }
            }
        }

        private void 充值金额_TextChanged(object sender, EventArgs e)
        {
            int imax = 200;
            if(money.Text !=null && money.Text != "")
            {
                if (int.Parse(money.Text) > imax)
                {
                    MessageBox.Show("充值金额不能大于200!");
                    money.Text = (0).ToString();
                }
            }
        }
 

题主说的优化具体指什么,性能优化,功能优化,还是代码优化

可以将数据库操作部分封装成一个SQLHelper类,这样的话就可以提高代码的复用性。

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632