添加项目文件。
This commit is contained in:
64
GeologyToolkit/MdbHelper.cs
Normal file
64
GeologyToolkit/MdbHelper.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user