How to connect access database in c#(c#中如何连接access数据库)
本文介绍了c#中如何连接access数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有访问数据库文件,其中包含 7 个表,但我不知道如何连接和显示所有表,如果有人可以帮助我?
I have access database file with 7 tables in it but I don't know how to connect and show all tables, If some one can help me?
这是我的代码,但它没有显示任何内容
this is my code but it doesn't show anything
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
String connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Tables.accdb;Persist Security Info=True";
string sql = "SELECT Clients FROM Tables";
conn.ConnectionString = connection;
conn.Open();
DataSet ds = new DataSet();
DataGridView dataGridView1 = new DataGridView();
BindingSource bSource = new BindingSource();
OleDbDataAdapter adapter = new OleDbDataAdapter(sql,conn);
adapter.Fill(ds);
//conn.Close();
dataGridView1.DataSource = ds;
推荐答案
试试这个代码,
public void ConnectToAccess()
{
System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection();
// TODO: Modify the connection string and include any
// additional required properties for your database.
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= C:Documents and Settingsusername" +
@"My DocumentsAccessFile.mdb";
try
{
conn.Open();
// Insert code to process data.
}
catch (Exception ex)
{
MessageBox.Show("Failed to connect to data source");
}
finally
{
conn.Close();
}
}
http://msdn.microsoft.com/en-us/库/5ybdbtte(v=vs.71).aspx
这篇关于c#中如何连接access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:c#中如何连接access数据库
基础教程推荐
猜你喜欢
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01