天天看点

运用.NIT将数据存入数据库、读取数据库(运用封装)陈老师作业

 我基础不好,根据所学的知识,书本的例题修改的,也不知道我理解的是否符合老师要求

运用C#将数据存入数据库、并且可以读取数据库里的数据,此项目我运用了封装。我运用了一个窗体将数据存读数据。

我首先创建了一个数据库,库名“数学题库” ,然后创建了一个表“tiku1”,

运用.NIT将数据存入数据库、读取数据库(运用封装)陈老师作业

添加一个类Class1

 class Class1

    {

        public string strCon = @"Data Source=.;Initial Catalog=数学题库;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 suanshu()

        {

            try

            {

                sqlcon = new SqlConnection(strCon);

            }

            catch (Exception e)

                MessageBox.Show("数据库连接不成功:" + e.ToString());

        }

        public void suanFill(string selstr)

            dt.Clear();

            sda = new SqlDataAdapter(selstr, strCon);

            sda.Fill(ds,"tiku1");

            dt = ds.Tables["tiku1"];

        public void suanselect(string showtnfo)

            sqlcon.Open();

            SqlCommand sqlcmd = new SqlCommand(showtnfo, sqlcon);

            sdr = sqlcmd.ExecuteReader();

        public void suanInsert(string insertinfo)

            SqlCommand sqlcmd = new SqlCommand(insertinfo, sqlcon);

                sqlcmd.ExecuteNonQuery();

                MessageBox.Show("数据插入失败:" + e.ToString());

            sqlcon.Close();

        public void suanGridViewUpd()

            SqlCommandBuilder scb = new SqlCommandBuilder(sda);

            DialogResult result;

            result = MessageBox.Show("确定保存修改过的数据吗?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (result == DialogResult.OK)

                dt = ds.Tables["tuku1"];

                sda.Update(dt);

                dt.AcceptChanges();

        public void suanDelete(string delStr)

            SqlCommand sqlcmd = new SqlCommand(delStr, sqlcon);

                MessageBox.Show("数据删除成功!");

            catch (Exception ex)

                MessageBox.Show("数据删除失败" + ex.ToString());

    }

}

 public partial class Form1 : Form

        public Form1()

            InitializeComponent();

        string selstr = @"select * from tiku1 ";

        Class1 db = new Class1();

 private void Form1_Load(object sender, EventArgs e)

            db.suanshu();

            db.suanFill(selstr);

            comboBox1.ValueMember = "序号";

            comboBox1.DataSource = db.dt.DefaultView;

 private void button4_Click(object sender, EventArgs e)

            dataGridView1.DataSource = db.dt;

 private void button1_Click(object sender, EventArgs e)

            string insertinfo = "insert into tiku1(序号,第一个数,符号一,第二个数,符号二,第三个数,符号三,结果) values('" + comboBox1.Text + "','" + textBox1.Text +

            "','" + textBox2.Text +

            "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text +

            "','" + textBox7.Text +

            "')";

            db.suanInsert(insertinfo);

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

            string selinfo = "select 第一个数,符号一,第二个数,符号二,第三个数,符号三,结果 from tiku1 where 序号='" + comboBox1.Text.ToString().Trim() + "'";

            db.suanselect(selinfo);

            while (db.sdr.Read())

          {                

            textBox1.Text = db.sdr["第一个数"].ToString();                

            textBox2.Text = db.sdr["符号一"].ToString();                

            textBox3.Text = db.sdr["第二个数"].ToString();                

            textBox4.Text = db.sdr["符号二"].ToString();                

            textBox5.Text = db.sdr["第三个数"].ToString();                

            textBox6.Text = db.sdr["符号三"].ToString();                

            textBox7.Text = db.sdr["结果"].ToString();

           db.sdr.Close();

 private void button3_Click(object sender, EventArgs e)

            string strupd = "delete from tiku1 where 序号='" + comboBox1.Text.Trim() + "'";

            db.suanDelete(strupd);

 private void button2_Click(object sender, EventArgs e)

            db.suanGridViewUpd();

     }

运用.NIT将数据存入数据库、读取数据库(运用封装)陈老师作业

说明

每道题我都编辑了序号,可以根据序号选题,改题,(序号我设置了下拉列表)

1、点击查询可以查询数据库里表里数据

2、点击添加可以添加数据

3、删除数据,更新数据