添加项目文件。
This commit is contained in:
88
ExportExcelTest/MdbHelper.cs
Normal file
88
ExportExcelTest/MdbHelper.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.OleDb;
|
||||
|
||||
using Bentley.Internal.MstnPlatformNET;
|
||||
|
||||
namespace GeologyToolkit
|
||||
{
|
||||
internal class MdbHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 构建函数
|
||||
/// </summary>
|
||||
/// <param name="fileName">MDB文件(含完整路徑)</param>
|
||||
public MdbHelper(string fileName)
|
||||
{
|
||||
this.fileName = fileName;
|
||||
connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName;
|
||||
}
|
||||
|
||||
private readonly string connectionString;
|
||||
private readonly string fileName;
|
||||
private OleDbConnection odcConnection;
|
||||
|
||||
/// <summary>
|
||||
/// 断开连接(关闭据库文件)
|
||||
/// </summary>
|
||||
public void Close()
|
||||
{
|
||||
odcConnection.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据sql命令返回一个DataSet
|
||||
/// </summary>
|
||||
/// <param name="sql">sql命令</param>
|
||||
/// <returns>以DataTable形式返回数据</returns>
|
||||
public DataSet GetDataSet(string sql)
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
|
||||
try
|
||||
{
|
||||
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, odcConnection);
|
||||
adapter.Fill(ds);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw new Exception("sql语句: " + sql + " 执行失败!");
|
||||
}
|
||||
|
||||
return ds;
|
||||
}
|
||||
//根据表名返回一个DataSet
|
||||
public DataSet GetDataSetByTableName(string tableName)
|
||||
{
|
||||
string sql = "select * from " + tableName;
|
||||
return GetDataSet(sql);
|
||||
}
|
||||
//查询所有的表名
|
||||
public DataTable GetTables()
|
||||
{
|
||||
DataTable dt = odcConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
|
||||
return dt;
|
||||
}
|
||||
public void Open12()
|
||||
{
|
||||
//string connStr = @"Driver={Microsoft Access Driver (*.mdb)};DBQ=" + fileName;
|
||||
string connectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={fileName};Persist Security Info=False;";
|
||||
using (OleDbConnection connection = new OleDbConnection(connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
// Your code to interact with the database
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 建立连接(打开数据库文件)
|
||||
/// </summary>
|
||||
public void Open()
|
||||
{
|
||||
// 建立连接
|
||||
odcConnection = new OleDbConnection(connectionString);
|
||||
// 打开连接
|
||||
odcConnection.Open();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user