Files
SzmediTools/GeologyToolkit/MdbHelper.cs
2025-09-16 16:06:41 +08:00

64 lines
1.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Data;
using System.Data.OleDb;
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;
}
/// <summary>
/// 建立连接(打开数据库文件)
/// </summary>
public void Open()
{
// 建立连接
odcConnection = new OleDbConnection(connectionString);
// 打开连接
odcConnection.Open();
}
}
}