这个access读写类在xp底下工作好好的,win7就不行,怎么改?
在xp底下好好地,在win7下一 this._conn.Open(); 就异常,怎么改?
using System;
using System.Data.OleDb;
using System.Data;
using System.Data.Sql;
using com.Db;
using com.Helper;
using com.Util;
using System.Text;
namespace com.Db.Access
{
public class AccessSqlDbImpl : ISqlDb
{
#region 成员
private OleDbConnection _conn;
private OleDbCommand _command;
private string serverip;
private string user;
private string password;
private string database;
#endregion
#region 构造函数
public AccessSqlDbImpl(string serverip, string user, string password, string database)
{
//if (serverip.ToLower().IndexOf(".mdb") < 0) serverip = serverip + ".mdb";
this.serverip = serverip; //这个为数据库的路径,为当前程序目录,比如:D:\\Db\\
this.user = user;
this.password = password;
this.database = database; //这个为Access数据库的目录名称,比如: abc.mdb
string DataSource = serverip;
string oleStr = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=\"" + serverip + "\";" +
"Persist Security Info=False;Jet OLEDB:DataBase Password=\"" + password + "\"";
this._conn = new OleDbConnection(oleStr);
this._command = _conn.CreateCommand();
}
#endregion
#region ISqlDb 成员
/// <summary>
/// 打开一个数据库连接
/// </summary>
/// <returns></returns>
public bool GetConnection()
{
try
{
this._conn.Open();
return true;
}
catch (OleDbException err)
{
byte[] msg = Encoding.Unicode.GetBytes(err.Message);
Log.Trace("调用GetConnection函数产生异常", msg, msg.Length, NumType.DecimalString);
return false;
}
catch (Exception err)
{
byte[] msg = Encoding.Unicode.GetBytes(err.Message);
Log.Trace("调用GetConnection函数产生异常", msg, msg.Length, NumType.DecimalString);
return false;
}
}
public void CloseConnect()
{
try
{
if (this._conn != null)
{
this._conn.Close();
}
}
catch (OleDbException err)
{
byte[] msg = Encoding.Unicode.GetBytes(err.Message);
Log.Trace("调用CloseConnect函数产生异常", msg, msg.Length, NumType.DecimalString);
}
catch (Exception err)
{
byte[] msg = Encoding.Unicode.GetBytes(err.Message);
Log.Trace("调用CloseConnect函数产生异常", msg, msg.Length, NumType.DecimalString);
}
}
public void BeginTransaction()
{
return;
}
public void RollBack()
{
return;
}
public void Commit()
{
return;
}
public DataSet Query(string sql)
{
this._command.CommandText = sql;
try
{
OleDbDataAdapter sda = new OleDbDataAdapter(_command);
//填充的时候带主键
//sda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
DataSet set = new DataSet();
sda.Fill(set, "T");
return set;
}
catch (OleDbException err)
{
byte[] msg = Encoding.Unicode.GetBytes(err.Message + " Sql:" + sql);
Log.Trace("调用Query函数产生OleDbException异常", msg, msg.Length, NumType.DecimalString);
return null;
}
catch (Exception err)
{
byte[] msg = Encoding.Unicode.GetBytes(err.Message);
Log.Trace("调用Query函数产生为知的异常", msg, msg.Length, NumType.DecimalString);
return null;
}
}
public DataSet Query(string sql, int startRow, int maxReturnRows)
{
this._command.CommandText = sql;
try
{
OleDbDataAdapter sda = new OleDbDataAdapter(sql, this._conn);
//填充的时候带主键
//sda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
DataSet set = new DataSet();
sda.Fill(set, startRow, maxReturnRows, "T");
return set;
}
catch (OleDbException err)
{
byte[] msg = Encoding.Unicode.GetBytes(err.Message);
Log.Trace("调用Query2函数产生异常", msg, msg.Length, NumType.DecimalString);
return null;
}
catch (Exception err)
{
byte[] msg = Encoding.Unicode.GetBytes(err.Message);
Log.Trace("调用Query2函数产生异常", msg, msg.Length, NumType.DecimalString);
return null;
}
}
public System.Data.DataTable GetDataTable(string sql, string tbname)
{
DataSet ds = Query(sql);
if (ds==null || ds.Tables==null || ds.Tables.Count == 0) return null;
ds.Tables["T"].TableName = tbname;
return ds.Tables[tbname];
}
public System.Data.DataRow GetDataRow(string sql)
{
DataTable dt = GetDataTable(sql, "T");
if (dt == null || dt.Rows.Count == 0) return null;
return dt.Rows[0];
}
/// <summary>
/// 执行一个数据插入、更新、删除命令,如果声明了事务就在事务中执行
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public int Excute(string sql)
{
this._command.CommandText = sql;
try
{
//对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。对于所有其他类型的语句,返回值为 -1。如果发生回滚,返回值也为 -1
return _command.ExecuteNonQuery();
}
catch (OleDbException err)
{
byte[] msg = Encoding.Unicode.GetBytes(err.Message);
Log.Trace("调用Excute函数产生异常", msg, msg.Length, NumType.DecimalString);
return -2;
}
catch (Exception err)
{
byte[] msg = Encoding.Unicode.GetBytes(err.Message);
Log.Trace("调用Excute函数产生异常", msg, msg.Length, NumType.DecimalString);
return -2;
}
}
#endregion
}
}