C#最基本的數據庫增刪改查


namespace access
{
    public partial class Form1 : Form
    {
        //定義數據庫的連接路徑
        string txtConn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\access.mdb";
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //修改記錄按鈕代碼
            using(OleDbConnection conn = new OleDbConnection(txtConn))
            {
                string sql = string.Format("update table1 set fl='{1}',lxfs='{2}',dzyj='{3}' where xm='{0}'", textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //刪除記錄按鈕代碼
            using (OleDbConnection conn = new OleDbConnection(txtConn))
            {
                string sql = string.Format("delete from table1 where xm='{0}'", textBox1.Text);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            //添加記錄按鈕代碼
            using (OleDbConnection conn = new OleDbConnection(txtConn))
            {
                string sql = string.Format("insert into table1(xm,fl,lxfs,dzyj) values('{0}','{1}','{2}','{3}')", textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
       
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM