核心代码
using System.Data.OleDb;
using System.Data.OleDb;
//GETDATATABLE
public static DataTable GetDatatable(string sql) {
//connsrt:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\fruit.mdb //Jet OLEDB:Database Password=
OleDbConnection conn = new OleDbConnection(MdbConnStr);
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
conn.Open();
OleDbDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
for (int i = 0; i < dr.FieldCount; i++) {
dt.Columns.Add(dr.GetName(i));
}
dt.Rows.Clear();
while (dr.Read()) {
DataRow row = dt.NewRow();
for (int i = 0; i < dr.FieldCount; i++) {
row[i] = dr[i];
}
dt.Rows.Add(row);
}
cmd.Dispose();
conn.Close();
return dt;
}
//执行SQL语句
public static int ExecuteNonQuery(string sql) {
//connsrt:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\fruit.mdb //Jet OLEDB:Database Password=
OleDbConnection conn = new OleDbConnection(MdbConnStr);
OleDbCommand cmd = conn.CreateCommand();
try {
cmd.CommandText = sql;
conn.Open();
int c = cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
return c;
} catch {
cmd.Dispose();
conn.Close();
return -1;
}
}
/// <summary>
/// 获取数据库架构信息
/// </summary>
/// <returns></returns>
public static DataTable GetSchema() {
OleDbConnection conn = new OleDbConnection(MdbConnStr);
try {
conn.Open();
DataTable schemaDt = conn.GetSchema();
conn.Close();
return schemaDt;
} catch (Exception ex) {
conn.Close();
throw new Exception(ex.ToString());
}
}
文章来源:https://www.toymoban.com/news/detail-594604.html
源码下载地址
https://download.csdn.net/download/qq_36403486/86401054文章来源地址https://www.toymoban.com/news/detail-594604.html
到了这里,关于C#调用Microsoft.Jet.OLEDB.4.0查看MDB数据库的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!