连接ACCESS数据库类
来源:互联网 发布:wine mac 编辑:程序博客网 时间:2024/06/05 00:26
using System;
using System.Data;
using System.Data.OleDb;
namespace Bussiness.Cls
{
/// <summary>
/// UtilHelper 的摘要说明
/// </summary>
public class UtilHelper
{
public UtilHelper()
{ }
public static string ConnString = @"Provider=Microsoft.Jet.OleDB.4.0;Data Source=" + Application.StartupPath + "//AnalyseFile//DB//DownLoad.mdb" + ";Persist Security Info=False;Jet OLEDB:Database Password=;";
private static string Password = "xyz_@#$%^9&8*4()!_+ooyq#$%";
/// <summary>
/// 执行SQL语句
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static OleDbDataReader ExecuteReader(string sql)
{
try
{
//创建数据库连接
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
//创建command对象并保存sql查询语句
OleDbCommand command = new OleDbCommand(sql, conn);
//创建datareader 对象来连接到表单
using (OleDbDataReader reader = command.ExecuteReader())
{
return reader;
}
}
}
//一些通常的异常处理
catch (OleDbException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 执行SQL语句
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static OleDbDataReader ExecuteReader(string sql, params OleDbParameter[] oledbparams)
{
try
{
//创建数据库连接
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
//创建command对象并保存sql查询语句
OleDbCommand command = new OleDbCommand(sql, conn);
// 增加参数
if (oledbparams != null)
{
foreach (OleDbParameter p in oledbparams)
{
if (p != null)
{
if ((p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input) && (p.Value == null))
{
p.Value = DBNull.Value;
}
command.Parameters.Add(p);
}
}
}
//创建datareader 对象来连接到表单
using (OleDbDataReader reader = command.ExecuteReader())
{
return reader;
}
}
}
//一些通常的异常处理
catch (OleDbException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 执行SQL语句
/// </summary>
/// <param name="sql"></param>
public static void ExecuteNonQuery(string sql)
{
try
{
//创建数据库连接
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
//创建command对象并保存sql查询语句
OleDbCommand command = new OleDbCommand(sql, conn);
if (conn.State == ConnectionState.Closed)
conn.Open();
//创建datareader 对象来连接到表单
command.ExecuteNonQuery();
conn.Close();
}
}
//一些通常的异常处理
catch (OleDbException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 执行SQL语句
/// </summary>
/// <param name="sql"></param>
public static void ExecuteNonQuery(string sql, params OleDbParameter[] oledbparams)
{
try
{
//创建数据库连接
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
//创建command对象并保存sql查询语句
// OleDbCommand command = new OleDbCommand(sql, conn);
OleDbCommand command = conn.CreateCommand();
command.CommandText = sql;
if (conn.State == ConnectionState.Closed)
conn.Open();
command.CommandType = CommandType.Text;
// 增加参数
if (oledbparams != null)
{
for (int i = 0; i < oledbparams.Length; i++)
{
command.Parameters.Add(oledbparams[i]);
}
}
//创建datareader 对象来连接到表单
command.ExecuteNonQuery();
conn.Close();
}
}
//一些通常的异常处理
catch (OleDbException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 执行SQL语句
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataSet ExecuteDataSet(string sql)
{
try
{
//创建数据库连接
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
if (conn.State == ConnectionState.Closed)
conn.Open();
using (OleDbDataAdapter ida = new OleDbDataAdapter(sql, conn))
{
DataSet ds = new DataSet();
ida.Fill(ds);
return ds;
}
}
}
//一些通常的异常处理
catch (OleDbException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
- 连接ACCESS数据库类
- JDBC连接数据库工具类及连接Access数据库示例
- JDBC连接数据库工具类及连接Access数据库示例
- .net连接ACCESS数据库的助手类
- C#的Access数据库的连接类
- PERL连接ACCESS数据库
- jsp连接ACCESS数据库
- javascript连接access数据库
- PHP连接ACCESS数据库
- asp连接Access数据库
- JavaScript连接Access数据库
- Java连接Access数据库
- C#连接Access数据库
- ASP连接Access数据库
- Delphi连接access数据库
- access连接远程数据库
- PHP连接ACCESS数据库
- Java连接Access数据库
- UltraWebGrid勾选记录相关操作
- 软件工程师总结(转)
- 获得中文星期名称
- 使用JUnit进行测试
- 清楚不能显示隐藏文件的注册表信息
- 连接ACCESS数据库类
- 红黑树
- Struts配置文件详解
- 防范网站数据库入侵
- 《深度探索C++对象模型》读书笔记3:C++中的多态,这跟指针有什么关系?
- c/c++中如何更改一个指针值
- 如果连接远程的mysql数据库连不上,可用下面的方法试下
- csdn的项目
- ubuntu 7.10 下 Qt 编程安装过程