打开任意位置的Access文件

先打开新建窗体,添加一个dataGridView控件,Access文件需保存为2000年版本左右

using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;


namespace 连接Access数据库
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.mdb|*.mdb";
            if(ofd.ShowDialog() == DialogResult.OK)
            {
                ofd.FileName=ofd.FileName;
            }
            string strPath = ofd.FileName;//获取Access数据库的所在路径
            string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + strPath;//设置数据库的连接字符串
            OleDbConnection  oleCon=new OleDbConnection(ConStr);//实例化OleDbConnection类
            OleDbDataAdapter oleDap= new OleDbDataAdapter("select*from 账目", oleCon);//SQL与数据库相连接
            DataSet ds=new DataSet();//实例化
            oleDap.Fill(ds);//添加SQL语句并执行
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;//显示数据
            oleCon.Dispose();
        }

       
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值