zoukankan      html  css  js  c++  java
  • 封装计算

    1:数据库连接及增、删、改、查方法:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Data;
    using System.Windows.Forms;
    
    namespace Windows10_10
    {
        class DBCon
        {
            public string strCon = @"Data Source=.;Initial Catalog=Solution; Integrated Security=true";
            public SqlConnection sqlCon = new SqlConnection();
            public SqlDataAdapter sda = new SqlDataAdapter();
            public DataSet ds = new DataSet();
            public DataTable dt = new DataTable();
            public SqlDataReader sdr;
            public void dbcon()
            {
                try
                {
                    sqlCon = new SqlConnection(strCon);
                }
                catch (Exception e)
                {
                    MessageBox.Show("数据连接不成功:" + e.ToString());
                }
            }
            public void dbFill(string selstr)
            {
                dt.Clear();
                sda = new SqlDataAdapter(selstr, strCon);
                sda.Fill(ds, "exam");
                dt = ds.Tables["exam"];
            }
            public void dbSelect(string showInfo)
            {
                sqlCon.Open();
                SqlCommand sqlcmd = new SqlCommand(showInfo, sqlCon);
                sdr = sqlcmd.ExecuteReader();
            }
            public void dbInsert(string insertInfo)
            {
                sqlCon.Open();
                SqlCommand sqlcmd = new SqlCommand(insertInfo, sqlCon);
                try
                {
                    sqlcmd.ExecuteNonQuery();
                    MessageBox.Show("增加成功!");
                }
                catch (Exception e)
                {
                    MessageBox.Show("数据插入失败" + e.ToString());
                }
                sqlCon.Close();
            }
            public void dbGridViewUpd()
            {
    
    
                SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                DialogResult result;
                result = MessageBox.Show("确定保存修改过的数据吗?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (result == DialogResult.OK)
                {
                    dt = ds.Tables["exam"];
                    sda.Update(dt);
                    dt.AcceptChanges();
    
                }
            }
            public void dbUpdate(string updStr)
            {
                sqlCon.Open();
                SqlCommand sqlcmd = new SqlCommand(updStr, sqlCon);
                try
                {
                    sqlcmd.ExecuteNonQuery();
                    MessageBox.Show("数据修改成功!");
    
                }
                catch (Exception e)
                {
                    MessageBox.Show("数据修改失败!" + e.ToString());
    
                }
                sqlCon.Close();
    
            }
            public void dbDelete(string delStr)
            {
                sqlCon.Open();
                SqlCommand sqlcmd = new SqlCommand(delStr, sqlCon);
                try
                {
                    sqlcmd.ExecuteNonQuery();
                   
    
                }
                catch (Exception e)
                {
                    MessageBox.Show("数据删除失败!" + e.ToString());
    
                }
                sqlCon.Close();
    
            }
        }
    }
    

     2,计算方法:

     class xyy
        {
            private int x1;
            private int x2;
            private int result;
            private char v;
           
            public xyy(int x1, int x2, char v)
            {
                this.x1 = x1;
                this.x2 = x2;
              
                this.v = v;
    
    
            }
            public int answer
            {
                get { return result; }
               
            
            }
            public int coom()
            {
                if (v == '+')
                {
                    result = x1 + x2;
                
                }
                else if (v == '-')
                {
                    result = x1 - x2;
                   
                }
                else if (v == '*')
                {
                    result = x1 * x2;
    
                }
                else if (v == '/')
                {
                    result = x1 / x2;
    
                }
                return result;
            }
    
        }
    

     3 ,Form1 及 增、删、改、查方法及计算方法调用:

    public partial class Form1 : Form
       {
            public Form1()
            {
                InitializeComponent();
            }
            
            public static int Count = 0;
            public static int t = 0;
            public static int right = 0;
            string selStr = @"select Num,n1,ys,n2,sum from exam";
            DBCon db = new DBCon();
    
            private void Form1_Load(object sender, EventArgs e)
            {
                db.dbcon();
                db.dbFill(selStr);
                comboBox1.ValueMember = "Num";
                comboBox1.DataSource = db.dt.DefaultView;
    
    
            }
          
          
    
            private void button2_Click(object sender, EventArgs e)
            {
                db.dbcon();
                db.dbFill(selStr);
                dataGridView1.DataSource = db.dt;
    
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                db.dbcon();
                string insertInfo = "insert into exam(Num,n1,ys,n2) values('" + comboBox1.Text +
                    "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
                db.dbInsert(insertInfo);
                db.dbFill(selStr);
                dataGridView1.DataSource = db.dt;
            }
    
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string selInfo = "select n1,ys,n2,sum from exam where Num='" + comboBox1.Text.ToString().Trim() + "'";
                db.dbcon();
                db.dbSelect(selInfo);
                if (db.sdr.Read())
                {
                    textBox1.Text = db.sdr["n1"].ToString();
                    textBox2.Text = db.sdr["ys"].ToString();
                    textBox3.Text = db.sdr["n2"].ToString();
                    textBox4.Text = db.sdr["sum"].ToString();
    
    
    
                }
    
    
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                
                db.dbcon();
                string strUpd = "update exam set n1='" + textBox1.Text.Trim() + "',ys='" + textBox2.Text.Trim() + "',n2='" +
                    textBox3.Text.Trim() + "'where Num='" + comboBox1.Text.Trim() + "'";
                db.dbUpdate(strUpd);
                db.dbFill(selStr);
                dataGridView1.DataSource = db.dt;
    
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                db.dbcon();
                string strUpd = "delete from exam where Num='" + comboBox1.Text.Trim() + "'";
                db.dbDelete(strUpd);
                MessageBox.Show("数据删除成功!");
                db.dbFill(selStr);
                dataGridView1.DataSource = db.dt;
    
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                db.dbcon();
                db.dbGridViewUpd();
                db.dbFill(selStr);
                dataGridView1.DataSource = db.dt;
            }
    
            private void button7_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
           
    
            private void button1_Click(object sender, EventArgs e)
            {
                textBox4.Enabled = false;
                timer1.Enabled =false;
                Form2 frm = new Form2();
                frm.ShowDialog();
            }
    
            private void button9_Click(object sender, EventArgs e)
            {
               
    
               
                label7.Text = t.ToString();
                timer1.Enabled = true;
                timer1.Interval = 1000;
                timer1.Start();
                
           
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                t = t + 1;
                label7.Text = t.ToString();
            }
    
            private void textBox4_KeyDown(object sender, KeyEventArgs e)
            {
                计算方法调用:
    int a = int.Parse(textBox1.Text.Trim()); int b=int.Parse(textBox3.Text.Trim()); Char c=Convert.ToChar(textBox2.Text.Trim()); xyy con = new xyy(a,b,c); con.coom(); if (e.KeyCode == Keys.Enter) { if (con.answer == int.Parse(textBox4.Text.Trim())) { MessageBox.Show("回答正确!"); right++; Count++; db.dbcon(); string strUpd = "update exam set n1='" + textBox1.Text.Trim() + "',ys='" + textBox2.Text.Trim() + "',n2='" + textBox3.Text.Trim() + "',sum='" + textBox4.Text.Trim() + "'where Num='" + comboBox1.Text.Trim() + "'"; db.dbUpdate(strUpd); db.dbFill(selStr); dataGridView1.DataSource = db.dt; } else { MessageBox.Show("答案错误,小朋请认真答题吆"); Count++; } textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); } } private void button8_Click(object sender, EventArgs e) { }
    }

     4 ,Form2

     private void Form2_Load(object sender, EventArgs e)
            {
                textBox1.Text = Form1.Count.ToString();
                textBox2.Text = Form1.right.ToString();
                textBox3.Text = ((Form1.right / (double)(Form1.Count)) * 100).ToString() + "%";
                textBox4.Text = ((Form1.Count / (double)(Form1.t)) * 100).ToString() + "%";
            }
    

  • 相关阅读:
    C++学习之路(四):线程安全的单例模式
    C++学习之路(三):volatile关键字
    C++学习之路(五):复制构造函数与赋值运算符重载
    类对象作为函数参数进行值传递
    System V共享内存介绍
    关于迭代器失效
    C++学习之路(二):引用
    C++学习之路(一):const与define,结构体对齐,new/delete
    epoll内核源码分析
    Redux中间件之redux-thunk使用详解
  • 原文地址:https://www.cnblogs.com/12345-xyy/p/4993116.html
Copyright © 2011-2022 走看看