赞
踩
先来个效果图
创建好Access数据库后,将其放入程序目录下(Debug模式就在debug下)
public partial class AccessForm : Form { public AccessForm() { InitializeComponent(); } public string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= mydatabase.accdb;"; private void button1_Click(object sender, EventArgs e) { string cmd = string.Format("select * from mylo"); DataSet dt = Query(cmd); } public DataSet Query(string strSql) { using (OleDbConnection conn = new OleDbConnection(ConnectionString)) { DataSet ds = new DataSet(); try { conn.Open(); OleDbDataAdapter command = new OleDbDataAdapter(strSql, conn); command.Fill(ds, "ds"); } catch (System.Data.SqlClient.SqlException ex) { throw; } return ds; } } public int ExecuteSql(string strSql) { using (OleDbConnection conn = new OleDbConnection(ConnectionString)) { using (OleDbCommand cmd = new OleDbCommand(strSql, conn)) { try { conn.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (System.Data.SqlClient.SqlException e) { conn.Close(); throw e; } } } } public object GetSingle(string strSql) { using (OleDbConnection conn = new OleDbConnection(ConnectionString)) { using (OleDbCommand cmd = new OleDbCommand(strSql, conn)) { try { conn.Open(); object obj = cmd.ExecuteScalar(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (System.Data.SqlClient.SqlException e) { conn.Close(); throw e; } } } } }
在查询过程中,出现报错
未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序。
解决:
1:百度accessdatabaseengine下载,安装。
2:原因: 使用的Provider和平台版本未匹配
转载:https://blog.csdn.net/w13922547238/article/details/106094936
我先是做了第一个处理,发现还是不行。然后做了第二个处理,才对。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。