C#连接操作ACCESS数据库实例代码 |

  • A+
所属分类:Seay信息安全博客

显示不全请点击全屏阅读

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace _1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //连接字符串解释:Provider=Microsoft.Jet.OleDb.4.0为指明数据引擎,data source为MDB文件位置,也就是数据源
            string str_con = “Provider=Microsoft.Jet.OleDb.4.0;Data Source=” + Application.StartupPath + @”1.mdb”;

            //创建OleDbConnection对象
            OleDbConnection con = new OleDbConnection(str_con);
            if (txt_name.Text == “”||txt_pass.Text==””)
            {
                MessageBox.Show(“请完整填写”,”操作提示”,MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }

            //定义SQL语句
            string str_sql = string.Format(“insert into admin values(‘{0}’,'{1}’)”,txt_name.Text,txt_pass.Text);
            con.Open();//打开连接

            //实例化OleDbCommand对象
            OleDbCommand cmd = new OleDbCommand(str_sql,con);

            int count = cmd.ExecuteNonQuery();//执行查询

            con.Close();//关闭连接
            if (count > 0)
            {
                MessageBox.Show(“添加成功”, “操作提示”, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                MessageBox.Show(“添加失败”, “操作提示”, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

                                                                                                                   来自博客:http://seay.sinaapp.com

Tags:

C#编程,

如果您喜欢我的博客,欢迎点击图片定订阅到邮箱填写您的邮件地址,订阅我们的精彩内容: 也可以点击链接【订阅到鲜果】

如果我的想法或工具帮助到了你,也可微信扫下方二维码打赏本人一杯咖啡
C#连接操作ACCESS数据库实例代码 |