尝试连接到项目的 C# 中的访问数据库时出错
Error when trying to connect to access database in C# for project
我正在尝试创建一个项目,该项目将在我的访问数据库文件中读取、写入和检查重复项。我正在使用 C# 并不断收到“连接失败错误,如果连接状态 = 0,我已将其写入程序。如果有人可以提供任何帮助,我将不胜感激。我正在使用 Access 2016,但我不确定我需要哪些参考资料对于我的项目(如果有的话)。我在网上找到的所有内容都已过时或不起作用。
谢谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.IO;
using System.Data.OleDb;
namespace RHG
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
using (OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.16.0;Data Source=C:\Users\grosales\Documents\rhg\RHG\Used.accdb"))
{
try
{
connection.Open();
MessageBox.Show("connected");
}
catch (Exception ex)
{
MessageBox.Show("connection failed");
}
}
}
`
您还没有打开连接。
connection.Open();
注意:检查连接状态是不够的。尝试打开连接时可能会出现异常。而是将其包含在 try-catch 中。
将工作包含在与 using
的连接中也是一个好习惯
using (var connection = new OleDbConnection()) {
connection.ConnectionString =
@"Provider=Microsoft.ACE.OLEDB.16.0;Data Source=C:\Users\...\Used.accdb";
try {
connection.Open();
//TODO: do something with the connection
} catch (Exception ex) {
MessageBox.Show("Connection Failed\r\n\r\n" + ex.Message);
}
}
这样可以确保关闭连接并释放资源。
尝试按照此处的示例操作:https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection(v=vs.110).aspx
你错过了 connection.Open();语句,应包含在 try catch 中。
此外,您可以在构造函数中指定连接字符串,例如
using (OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.16.0;Data Source=C:\Users\grosales\Documents\rhg\RHG\Used.accdb"))
{
//do DB access here
}
//no need to call connection.Close() - it's automatically done once you leave the using block.
我正在尝试创建一个项目,该项目将在我的访问数据库文件中读取、写入和检查重复项。我正在使用 C# 并不断收到“连接失败错误,如果连接状态 = 0,我已将其写入程序。如果有人可以提供任何帮助,我将不胜感激。我正在使用 Access 2016,但我不确定我需要哪些参考资料对于我的项目(如果有的话)。我在网上找到的所有内容都已过时或不起作用。
谢谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.IO;
using System.Data.OleDb;
namespace RHG
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
using (OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.16.0;Data Source=C:\Users\grosales\Documents\rhg\RHG\Used.accdb"))
{
try
{
connection.Open();
MessageBox.Show("connected");
}
catch (Exception ex)
{
MessageBox.Show("connection failed");
}
}
}
`
您还没有打开连接。
connection.Open();
注意:检查连接状态是不够的。尝试打开连接时可能会出现异常。而是将其包含在 try-catch 中。
将工作包含在与 using
using (var connection = new OleDbConnection()) {
connection.ConnectionString =
@"Provider=Microsoft.ACE.OLEDB.16.0;Data Source=C:\Users\...\Used.accdb";
try {
connection.Open();
//TODO: do something with the connection
} catch (Exception ex) {
MessageBox.Show("Connection Failed\r\n\r\n" + ex.Message);
}
}
这样可以确保关闭连接并释放资源。
尝试按照此处的示例操作:https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection(v=vs.110).aspx
你错过了 connection.Open();语句,应包含在 try catch 中。
此外,您可以在构造函数中指定连接字符串,例如
using (OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.16.0;Data Source=C:\Users\grosales\Documents\rhg\RHG\Used.accdb"))
{
//do DB access here
}
//no need to call connection.Close() - it's automatically done once you leave the using block.