天天看點

運用.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、删除資料,更新資料