我写的一个类,注意二进制文件操作的部分不通用,需要根据具体情况去改
using System;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using System.Collections;
using System.Windows.Forms;
namespace AccessOperator
{
public class AccessHelper
{
#region 属性
private OleDbConnection conn;
// private OleDbCommand cmd;
#endregion
#region 打开Access
/// <summary>
/// 打开连接
/// </summary>
/// <param name="AccessPath">完整路径,//</param>
/// <param name="UserName">无用户名,则参数为""</param>
/// <param name="UserPWD">无密码,则参数为""</param>
public AccessHelper(string AccessPath, string UserName, string UserPWD)
{
try
{
//组成数据库连接字符串
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + AccessPath + ";";
if (UserName != string.Empty && UserPWD != string.Empty)
{
ConnStr += "User Id=" + UserName + "; Password=" + UserPWD + ";";
}
conn = new OleDbConnection(ConnStr);
conn.Open();
if (conn.State != ConnectionState.Open)
{
MessageBox.Show("数据库连接异常");
}
}
catch (Exception ex)
{
MessageBox.Show("数据库连接异常,请确保Access文件存在,用户名、密码正确");
}
}
#endregion
/// <summary>
/// 返回受影响的行数
/// </summary>
/// <param name="cmdText">SQL 语句</param>
/// <returns>返回受影响的函数</returns>
public int ExecuteNonQuery(string cmdText)
{
OleDbCommand cmd &#