access数据库的连接类问题?

hao_t1021 2010-07-27 10:10:10
我在网上找到一个DB类,改了一下,可是连接字符串不对,请哪位懂的帮忙改改!如果谁有能用的access数据库的连接类给小弟贴一个,万分感谢!我用的是VS.2005

namespace carAccess
{

public class OleDbHelper
{
public OleDbHelper(){ }

/// <summary>
/// 返回数据库连接字符串
/// </summary>
/// <returns></returns>
public static String GetSqlConnection()
{
//System.Web.UI.Page page = new System.Web.UI.Page();
String conn = new OleDbConnection(System.Configuration.ConfigurationManager.AppSettings["DBLink"].ToString() );
//这里有错


return conn;
}

/// <summary>
/// 获得参数对象
/// </summary>
/// <param name="paramName">参数名称</param>
/// <param name="paramType">数据类型</param>
/// <param name="paramSize">长度</param>
/// <param name="ColName">源列名称</param>
/// <param name="paramValue">参数实值</param>
/// <returns></returns>
public static OleDbParameter GetParameter(String paramName, OleDbType paramType, Int32 paramSize, String ColName, Object paramValue)
{
OleDbParameter param = new OleDbParameter(paramName, paramType, paramSize, ColName);
param.Value = paramValue;
return param;
}

/// <summary>
/// 获得参数对象
/// </summary>
/// <param name="paramName">参数名称</param>
/// <param name="paramType">数据类型</param>
/// <param name="paramSize">长度</param>
/// <param name="ColName">源列名称</param>
/// <returns></returns>
public static OleDbParameter GetParameter(String paramName, OleDbType paramType, Int32 paramSize, String ColName)
{
OleDbParameter param = new OleDbParameter(paramName, paramType, paramSize, ColName);
return param;
}

/// <summary>
/// 获得参数对象
/// </summary>
/// <param name="paramName">参数名称</param>
/// <param name="paramType">数据类型</param>
/// <param name="paramSize">长度</param>
/// <param name="ColName">源列名称</param>
/// <returns></returns>
public static OleDbParameter GetParameter(String paramName, OleDbType paramType, Object paramValue)
{
OleDbParameter param = new OleDbParameter(paramName,paramType);
param.Value = paramValue;
return param;
}

/// <summary>
/// 执行SQL语句
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <param name="param">参数对象数组</param>
/// <returns></returns>
public static int ExecuteSql(String Sqlstr, OleDbParameter[] param)
{
String ConnStr = OleDbHelper.GetSqlConnection();
using (OleDbConnection conn = new OleDbConnection(ConnStr))
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
cmd.Parameters.AddRange(param);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
return 1;
}
}

/// <summary>
/// 执行SQL语句并返回数据表
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <returns></returns>
public static DataTable ExecuteDt(String Sqlstr)
{
String ConnStr = OleDbHelper.GetSqlConnection();
using (OleDbConnection conn = new OleDbConnection(ConnStr))
{
OleDbDataAdapter da = new OleDbDataAdapter(Sqlstr, conn);
DataTable dt = new DataTable();
conn.Open();
da.Fill(dt);
conn.Close();
return dt;
}
}

/// <summary>
/// 执行SQL语句并返回数据表
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <param name="param">参数对象列表</param>
/// <returns></returns>
public static DataTable ExecuteDt(String Sqlstr, OleDbParameter[] param)
{
String ConnStr = OleDbHelper.GetSqlConnection();
using (OleDbConnection conn = new OleDbConnection(ConnStr))
{
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
OleDbCommand cmd = new OleDbCommand(Sqlstr,conn);//执行语句
cmd.Connection = conn;
cmd.Parameters.AddRange(param);//加入参数列表
da.SelectCommand = cmd;
conn.Open();
da.Fill(dt);
conn.Close();
return dt;
}
}

/// <summary>
/// 批量执行SQL语句
/// </summary>
/// <param name="Sqlstr">SQL语句数组</param>
/// <param name="param">SQL参数对象数组</param>
/// <returns></returns>
public static Int32 ExecuteSqls(String [] Sqlstr,List<OleDbParameter []> param)
{
String ConnStr = OleDbHelper.GetSqlConnection();
using (OleDbConnection conn = new OleDbConnection(ConnStr))
{

OleDbCommand cmd = new OleDbCommand();
OleDbTransaction tran = null;
cmd.Transaction = tran;
try
{
conn.Open();
tran = conn.BeginTransaction();
cmd.Connection = conn;
cmd.Transaction = tran;

Int32 count = Sqlstr.Length;
for (Int32 i = 0; i < count; i ++ )
{
cmd.CommandText = Sqlstr[i];
cmd.Parameters.AddRange(param[i]);
cmd.ExecuteNonQuery();
}
tran.Commit();
return 1;
}
catch
{
tran.Rollback();
return 0;
}
finally
{
cmd.Dispose();
conn.Close();
}
}
}
}
}
...全文
99 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
leiziaitudou 2010-07-27
  • 打赏
  • 举报
回复
连接字符串:
String conn = new OleDbConnection(System.Configuration.ConfigurationManager.AppSettings["DBLink"] +DbPath );//删掉,配置文件可以不用删,你试一试
配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SQLConnString" value="provider=microsoft.jet.oledb.4.0;data source=./CarBeauty.mdb"/>
<add key="dbPath" value="~/CarBeauty.mdb"/> //看这没有必要
</appSettings>
</configuration>
hao_t1021 2010-07-27
  • 打赏
  • 举报
回复
连接字符串:
String conn = new OleDbConnection(System.Configuration.ConfigurationManager.AppSettings["DBLink"]+DbPath );
配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SQLConnString" value="provider=microsoft.jet.oledb.4.0;data source=./CarBeauty.mdb"/>
<add key="dbPath" value="~/CarBeauty.mdb"/>
</appSettings>
</configuration>

请问是这样写吗?提示不存在名称dbPath
wuyq11 2010-07-27
  • 打赏
  • 举报
回复
<add name="accessConnStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source= |DataDirectory|a.mdb" providerName="System.Data.OleDb"/>

<appSettings>
<add key="ConnString" value="provider=microsoft.jet.oledb.4.0;data source="/>
<add key="dbPath" value="~/App_Data/a.mdb"/>
</appSettings>
mafgm 2010-07-27
  • 打赏
  • 举报
回复
在配置文件中设置DBLink的值为:"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Gather.mdb;Jet OLEDB:Database Password=123"
kinghongchen 2010-07-27
  • 打赏
  • 举报
回复
connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "rtisp.mdb";
dataCon = new OleDbConnection(connectionString);
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hao_t1021 的回复:]
我现在改为

C# code
String conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString);


可是又提示


C# code
无法将类型“System.Data.OleDb.OleDb……
[/Quote]
OleDbConnection con=new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString);
hao_t1021 2010-07-27
  • 打赏
  • 举报
回复
我现在改为
String conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString);

可是又提示
无法将类型“System.Data.OleDb.OleDbConnection”隐式转换为“string”

111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧