展开全部
这个跟SQL SERVER 一样。只是命名空间不一样
下面的代码供你参考。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
namespace SQLHelper
{
public class AccessDBHlper
{
/// <summary>
/// 返回数据库连接字符串
/// </summary>
/// <returns></returns>
public static String GetOleDbConnection()
{
//String conn = ""; 链接字符串
String conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
return conn;
}
/// <summary>
/// 获取动态字符串
/// </summary>
/// <param name="constr">连接字符串名称</param>
/// <returns></returns>
public static OleDbConnection sc(string constr)
{
OleDbConnection sc = new OleDbConnection(constr);
sc.Open();
return sc;
}
/// <summary>
/// 获取连接对象
/// </summary>
/// <returns></returns>
public static OleDbConnection GetCon()
{
OleDbConnection sc = new OleDbConnection(GetOleDbConnection());
sc.Open();
return sc;
}
/// <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, SqlDbType paramType, Object paramValue)
{
OleDbParameter param = new OleDbParameter(paramName, paramType);
param.Value = paramValue;
return param;
}
public static int ExecuteSql(String Sqlstr)
{
String ConnStr = GetOleDbConnection();
using (OleDbConnection conn = new OleDbConnection(ConnStr))
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
conn.Open();
int counter;
counter = cmd.ExecuteNonQuery();
conn.Close();
return counter;
}
}
/// <summary>
/// 执行SQL语句
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <param name="param">参数对象数组</param>
/// <returns></returns>
public static int ExecuteSql(String Sqlstr, OleDbParameter[] param)
{
String ConnStr = GetOleDbConnection();
using (OleDbConnection conn = new OleDbConnection(ConnStr))
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
cmd.Parameters.AddRange(param);
conn.Open();
int counter;
counter = cmd.ExecuteNonQuery();
conn.Close();
return counter;
}
}
/// <summary>
/// 执行SQL语句并返回数据表
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <returns></returns>
public static DataTable ExecuteDt(String Sqlstr)
{
String ConnStr = GetOleDbConnection();
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;
}
}
}
下面的代码供你参考。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
namespace SQLHelper
{
public class AccessDBHlper
{
/// <summary>
/// 返回数据库连接字符串
/// </summary>
/// <returns></returns>
public static String GetOleDbConnection()
{
//String conn = ""; 链接字符串
String conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
return conn;
}
/// <summary>
/// 获取动态字符串
/// </summary>
/// <param name="constr">连接字符串名称</param>
/// <returns></returns>
public static OleDbConnection sc(string constr)
{
OleDbConnection sc = new OleDbConnection(constr);
sc.Open();
return sc;
}
/// <summary>
/// 获取连接对象
/// </summary>
/// <returns></returns>
public static OleDbConnection GetCon()
{
OleDbConnection sc = new OleDbConnection(GetOleDbConnection());
sc.Open();
return sc;
}
/// <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, SqlDbType paramType, Object paramValue)
{
OleDbParameter param = new OleDbParameter(paramName, paramType);
param.Value = paramValue;
return param;
}
public static int ExecuteSql(String Sqlstr)
{
String ConnStr = GetOleDbConnection();
using (OleDbConnection conn = new OleDbConnection(ConnStr))
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
conn.Open();
int counter;
counter = cmd.ExecuteNonQuery();
conn.Close();
return counter;
}
}
/// <summary>
/// 执行SQL语句
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <param name="param">参数对象数组</param>
/// <returns></returns>
public static int ExecuteSql(String Sqlstr, OleDbParameter[] param)
{
String ConnStr = GetOleDbConnection();
using (OleDbConnection conn = new OleDbConnection(ConnStr))
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
cmd.Parameters.AddRange(param);
conn.Open();
int counter;
counter = cmd.ExecuteNonQuery();
conn.Close();
return counter;
}
}
/// <summary>
/// 执行SQL语句并返回数据表
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <returns></returns>
public static DataTable ExecuteDt(String Sqlstr)
{
String ConnStr = GetOleDbConnection();
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;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询