数据库文件储存

{
        public Form1()
        {
            InitializeComponent();
        }
        public static string strCon = "Data Source=.;Initial Catalog=calculator;Integrated Security=True";
          public  SqlConnection sqlCon = new SqlConnection(strCon);
            

        private void button1_Click(object sender, EventArgs e)
        {
            
            sqlCon.Open();
            string inster = "insert into Table_1(number,frist,operator,two) values('"+comboBox2.Text+"','"+textBox1.Text+"','"+comboBox1.Text+"','"+textBox3.Text+"')";
            SqlCommand sqlCmd =new SqlCommand(inster,sqlCon);
           int a =sqlCmd.ExecuteNonQuery();
            if (a > 0)
            {
                MessageBox.Show("数据插入成功");
            }
            else
            {
                MessageBox.Show("数据插入失败");
            }
            string selec = "select * from Table_1";
            SqlDataAdapter ds = new SqlDataAdapter(selec, sqlCon);
            DataSet sda = new DataSet();
            ds.Fill(sda, "Table_1");
            DataTable dt = sda.Tables["Table_1"];
            dataGridView1.DataSource = dt;
        }

     

        private void Form1_Load(object sender, EventArgs e)
        {
           
            sqlCon.Open();
            string select = "select * from Table_1";
            SqlDataAdapter ds = new SqlDataAdapter(select, sqlCon);
            DataSet sda = new DataSet();
            ds.Fill(sda, "Table_1");
            DataTable dt = sda.Tables["Table_1"];
            comboBox2.ValueMember = "number";
            comboBox2.DataSource = dt.DefaultView;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            sqlCon.Open();
            string select = "select * from Table_1";
            SqlDataAdapter ds = new SqlDataAdapter(select,sqlCon);
            DataSet sda = new DataSet();
            ds.Fill(sda, "Table_1");
            DataTable dt = sda.Tables["Table_1"];
            dataGridView1.DataSource = dt;

        }

        private void button4_Click(object sender, EventArgs e)
        {
          
            sqlCon.Open();
            string select = "delete from Table_1 where number='" + comboBox2.Text + "'";
            SqlCommand sd = new SqlCommand(select,sqlCon);
            int a=sd.ExecuteNonQuery();
            if (a > 0)
            {
                MessageBox.Show("数据删除成功");
            }
            else
            {
                MessageBox.Show("数据删除失败");
            }
            string selec = "select * from Table_1";
            SqlDataAdapter ds = new SqlDataAdapter(selec, sqlCon);
            DataSet sda = new DataSet();
            ds.Fill(sda, "Table_1");
            DataTable dt = sda.Tables["Table_1"];
            dataGridView1.DataSource = dt;
        }

     

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
          
            sqlCon.Open();
            string select = "select frist,operator,two from Table_1 where number='"+comboBox2+"'";
            SqlCommand cmd = new SqlCommand(select,sqlCon);
            SqlDataReader sdr =cmd.ExecuteReader();

            while (sdr.Read())
            { 
             textBox1.Text=sdr["frist"].ToString();
             comboBox1.Text = sdr["operator"].ToString();
             textBox3.Text = sdr["two"].ToString();

            
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            calculate sa = new calculate(int.Parse(textBox1.Text), int.Parse(textBox3.Text), comboBox1.Text);
            if (sa.answer == int.Parse(textBox4.Text))
            {
                MessageBox.Show("回答正确");
            }
            else
            {
                MessageBox.Show("回答错误");

            }
        }

       

       
    }
}

  

  interface chapter
    {
        int answer { get; }
    }
    class calculate : chapter
    {
        private int x1;
        private int x2;
        private string Operator;
        public int result;
        public calculate(int x1, int x2, string Operator)
        {
            this.x1 = x1;
            this.x2 = x2;
            this.Operator = Operator;


        }
        public int answer
        {
            get
            {

                if (Operator == "+")
                {
                    result = x1 + x2;

                }
                if (Operator == "-")
                {

                    result = x1 - x2;

                }
                if (Operator == "*")
                {

                    result = x1 * x2;

                }
                if (Operator == "/")
                {

                    result = x1 / x2;

                }
                return result;
            }
        }

    }
}

总结

 

这主要数据库的运用也还好比较容易,革命尚未成功,我们仍需努力。。

posted @ 2015-12-13 22:47  希望永远在明天  阅读(389)  评论(1编辑  收藏  举报