码迷,mamicode.com
首页 > 数据库 > 详细

C#最基本的数据库增删改查

时间:2017-01-17 13:58:01      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:provider   mat   定义   date   using   new   ring   format   into   

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();
            }
        }
       
    }
}

 

C#最基本的数据库增删改查

标签:provider   mat   定义   date   using   new   ring   format   into   

原文地址:http://www.cnblogs.com/wzwyc/p/6292670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!