c# - 无法在数据源列表中找到 OLEDB 提供程序
问题描述
我正在尝试在我的项目中添加一个 ADO.net 实体数据模型。我有 accdb 文件。当我尝试在数据源中添加它时,无法在列表中获取 OLEDB 提供程序。
我已经拥有 Microsoft Access 引擎提供程序。我的系统上缺少什么。请帮我。任何帮助将不胜感激。
解决方案
我不确定这是否会将连接作为下拉项提供给您,但这些代码示例向您展示了几种连接到 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 ! ");
}
}
}
}
推荐阅读
- php - ajax 在第一次单击时不起作用,在 2 次单击后运行多次
- android - 将超过 15 GB 的视频文件从 android 上传到 node.js 服务器
- c# - 基于通用参数将记录添加到 ICollection
- c - 为什么 MSVC 调试模式会为一个空的 if() 主体而不是另一个(i++ 与 ++i)遗漏 cmp/jcc?
- c++ - 从工厂方法返回的 const unique_ptr 不需要空检查?
- c# - DLLImport Fortran 到 C#
- postgresql - 如何在 WHERE 子句中使用 age() 函数
- mongodb - .NET 核心项目中的 Mongodb 多租户
- database - react native 组件中的长网络操作
- c++ - 大量 QMenus 的基于 for 的循环