首页 > 解决方案 > 无法在数据源列表中找到 OLEDB 提供程序

问题描述

我正在尝试在我的项目中添加一个 ADO.net 实体数据模型。我有 accdb 文件。当我尝试在数据源中添加它时,无法在列表中获取 OLEDB 提供程序。

在此处输入图像描述

我已经拥有 Microsoft Access 引擎提供程序。我的系统上缺少什么。请帮我。任何帮助将不胜感激。

标签: c#visual-studioms-accessoledb

解决方案


我不确定这是否会将连接作为下拉项提供给您,但这些代码示例向您展示了几种连接到 MS Access 的方法。

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            OleDbConnection oledbCnn ;
            OleDbDataAdapter oledbAdapter ;
            DataSet ds = new DataSet();
            string sql = null;
            int i = 0;

            connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;";
            sql = "Your SQL Statement Here like Select * from product"; 

            oledbCnn = new OleDbConnection(connetionString);
            try
            {
                oledbCnn.Open();
                oledbAdapter = new OleDbDataAdapter(sql, oledbCnn);
                oledbAdapter.Fill(ds);
                for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + " -- " + ds.Tables[0].Rows[i].ItemArray[1]);
                }
                oledbAdapter.Dispose();
                oledbCnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }
        }
    }
}

或者

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            OleDbConnection oledbCnn ;
            OleDbCommand oledbCmd ;
            string sql = null;

            connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;";
            sql = "Your SQL Statement Here like Select * from product";

            oledbCnn = new OleDbConnection(connetionString);
            try
            {
                oledbCnn.Open();
                oledbCmd = new OleDbCommand(sql, oledbCnn);
                OleDbDataReader oledbReader = oledbCmd.ExecuteReader();
                while (oledbReader.Read ())
                {
                    MessageBox.Show(oledbReader.GetValue(0) + " - " + oledbReader.GetValue(1) + " - " + oledbReader.GetValue(2));
                }
                oledbReader.Close();
                oledbCmd.Dispose();
                oledbCnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }
        }
    }
}

推荐阅读