C# System.Data.OleDb.OleDbException
本文关键字:OleDbException OleDb Data System | 更新日期: 2023-09-27 18:12:06
我是c#新手,正在尝试连接到。accdb访问2010数据库
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
OleDbConnection connect = new OleDbConnection();
connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:'Users'Web Develop'Documents'Storekeeper'storekeeper.accdb;Persist Security Info=False;";
connect.Open();
MessageBox.Show("Connection open");
}
}
}
,我得到了这个异常:
在System.Data.dll中出现System.Data.OleDb.OleDbException
类型的第一次机会异常
数据库没有被使用,路径是正确的,我该怎么办?
在抛出的异常上应该有一个可以检查的InnerException
属性。它会告诉你准确的误差是多少。要查看它,需要捕获异常,然后显示InnerException消息:
private void Form1_Load(object sender, EventArgs e)
{
try
{
OleDbConnection connect = new OleDbConnection();
connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:'Users'Web Develop'Documents'Storekeeper'storekeeper.accdb;Persist Security Info=False;";
connect.Open();
MessageBox.Show("Connection open");
}
catch (OleDbException e)
{
Messagebox.Show(e.InnerException.Message);
}
}
有额外的示例代码捕获和显示错误嵌入在一个OleDbException
在MSDN页oledbeception
我觉得,这很简单。由于你对office 2010,我相信你需要:Microsoft.ACE.OLEDB.14.0
好的。如果你用的是32位的Office在64位的O/S上,那么你就会有问题。尝试将"平台输出"更改为x86。转到您的项目属性并找到"Build"选项卡。"平台目标"应该列在那里。
现在,即使这有效,你也必须调查这个决定的后果。但至少"你会知道的"。
编辑 --------------
这是你的排列。你只需要用它们来试验。
-
连接字符串,正确还是错误。就像之前提到的"12"answers"14"(抱歉,这个链接是关于Excel的。
-
正在安装Office 32位。我想如果你试图在那台机器上安装"AccessDatabaseEngine_x64.exe",它会给你一个"Office版本不正确"的错误。
-
所以因为#2,你必须安装"AccessDatabaseEngine.exe"。
-
"平台输出"。现在我认为,由于#3的原因,您需要尝试将其设置为x86。
尝试将其放回x86,然后尝试连接字符串中的"12"与"14"。
编辑 -----------------
我从网上下载了一个文件。
Oren.accdb从http://old.cba.ua.edu/~ jomason/ac289/289AccessFiles.html
我在我的机器上编码了这个。
private void button1_Click(object sender, EventArgs e)
{
try
{
using (OleDbConnection connect = new OleDbConnection())
{
connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:'folder1'data'Oren.accdb;Persist Security Info=False;";
connect.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = connect;
cmd.CommandText = "Select * from Agreement";
StringBuilder sb = new StringBuilder();
IDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader[0].ToString());
sb.Append(string.Format("{0}, {1}", reader[0].ToString(), reader[1].ToString()) + System.Environment.NewLine);
}
reader.Close();
ReportMessage(sb.ToString());
}
}
catch (System.Data.OleDb.OleDbException lolex)
{
ReportException(lolex);
}
catch (Exception ex)
{
ReportException(ex);
}
}
private void ReportException(Exception ex)
{
txtStatus.Text = ex.Message;
}
private void ReportException(System.Data.OleDb.OleDbException oleex)
{
StringBuilder sb = new StringBuilder();
sb.Append(oleex.ErrorCode + System.Environment.NewLine);
sb.Append(oleex.Message + System.Environment.NewLine );
txtStatus.Text = sb.ToString();
}
private void ReportMessage(string msg)
{
txtStatus.Text = msg;
}
编辑你能打开"storekeeper. net "文件吗?"Microsoft Access"程序中的accdb"。没有密码保护,对吧?
您需要在连接字符串上的数据源路径上放置双斜杠,例如:'Data Source=C:'folder1' Data 'Oren.accdb;Persist Security Info=False;";'