连接Access数据库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.OleDb; namespace TestAccessDB
{ public class DBHelper
{
protected static OleDbConnection conn = new OleDbConnection();
protected static OleDbCommand comm = new OleDbCommand(); public DBHelper()
{ }
static string dbFile = AppDomain.CurrentDomain.BaseDirectory + "\\goods.mdb"; private static void openConnection()
{
if (conn.State == ConnectionState.Closed)
{
// conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + dbFile + ";Jet OLEDB:Database PassWord=sa";
// conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ dbFile +";Persist Security Info=False;Jet OLEDB:Database Password=sa;";
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbFile + ";Persist Security Info=False;";
comm.Connection = conn;
try
{
conn.Open();
}
catch (Exception e)
{ throw new Exception(e.Message); } } }
private static void closeConnection()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
comm.Dispose();
}
}
/// <summary>
/// 执行sql语句
/// </summary>
/// <param name="sqlstr"></param>
public static int ExecuteNonQuery(string sqlstr)
{
int i = 0;
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
i= comm.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{ closeConnection(); }
return i;
} /// <summary>
/// 执行sql语句
/// </summary>
/// <param name="sqlstr"></param>
public static object executeScalarSql(string sqlstr)
{
object o;
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
o= comm.ExecuteScalar();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{ closeConnection(); } return o;
} public static int batchInsert(int count)
{ int cnt = 0; try
{ for (int i = 1; i <= count; i++)
{
string sqlstr = "insert into article(sourceID,[user],updateDate) values ('454545434','qwertteeeew_gfhfghgfh',#" + DateTime.Now + "#)"; openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
cnt += comm.ExecuteNonQuery(); } }
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{ closeConnection(); } return cnt; } /// <summary>
/// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。
/// </summary>
/// <param name="sqlstr"></param>
/// <returns></returns>
public static OleDbDataReader dataReader(string sqlstr)
{
OleDbDataReader dr = null;
try
{
openConnection();
comm.CommandText = sqlstr;
comm.CommandType = CommandType.Text; dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
dr.Close();
closeConnection();
}
catch { }
}
return dr;
}
/// <summary>
/// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭
/// </summary>
/// <param name="sqlstr"></param>
/// <param name="dr"></param>
public static void dataReader(string sqlstr, ref OleDbDataReader dr)
{
try
{
openConnection();
comm.CommandText = sqlstr;
comm.CommandType = CommandType.Text;
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
if (dr != null && !dr.IsClosed)
dr.Close();
}
catch
{
}
finally
{
closeConnection();
}
}
}
/// <summary>
/// 返回指定sql语句的dataset
/// </summary>
/// <param name="sqlstr"></param>
/// <returns></returns>
public static DataSet dataSet(string sqlstr)
{
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds); }
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return ds;
}
/// <summary>
/// 返回指定sql语句的dataset
/// </summary>
/// <param name="sqlstr"></param>
/// <param name="ds"></param>
public static void dataSet(string sqlstr, ref DataSet ds)
{
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
/// <summary>
/// 返回指定sql语句的datatable
/// </summary>
/// <param name="sqlstr"></param>
/// <returns></returns>
public static DataTable dataTable(string sqlstr)
{
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return dt;
}
/// <summary>
/// 返回指定sql语句的datatable
/// </summary>
/// <param name="sqlstr"></param>
/// <param name="dt"></param>
public static void dataTable(string sqlstr, ref DataTable dt)
{
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
/// <summary>
/// 返回指定sql语句的dataview
/// </summary>
/// <param name="sqlstr"></param>
/// <returns></returns>
public static DataView dataView(string sqlstr)
{
OleDbDataAdapter da = new OleDbDataAdapter();
DataView dv = new DataView();
DataSet ds = new DataSet();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
dv = ds.Tables[0].DefaultView;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return dv;
}
}
}

  

最新文章

  1. Entity Framework 6 Recipes 2nd Edition(13-8)译 -&gt; 把昂贵的属性移到其它实体
  2. 笔记 .Net反射机制
  3. svm机器学习算法中文视频讲解
  4. 三. 动态添加option选项
  5. CSS之盒子模型及常见布局
  6. (六) 一起学 Unix 环境高级编程 (APUE) 之 进程控制
  7. 安装DRBD的一些问题
  8. switchomega配置
  9. 【转】vnc centos
  10. 关于《一步步学习ASP.NET MVC3》系列发布时间的说明
  11. Javascript中的onclick事件
  12. 推测的手机型号和cpu模型
  13. 学会Func
  14. 洛谷P1776--宝物筛选(单调队列+多重背包)
  15. NotBacon
  16. [原创]Java性能优化权威指南读书思维导图4
  17. kernel_thread简析
  18. hdu 2588(简单的欧拉
  19. window10删除导航栏中的onedrive
  20. Windows Security Login

热门文章

  1. ajax异步加载分页评论带点赞功能
  2. tomcat 启动报错 解决办法 A child container failed during&amp;nbsp
  3. SPSS 23下载安装和激活
  4. linux Apache 日志轮询
  5. Codeforces Round #427 (Div. 2) - A
  6. PAT Advanced 1046 Shortest Distance (20 分) (知识点:贪心算法)
  7. 2019长安大学ACM校赛网络同步赛C LaTale (树上DP)
  8. jmeter性能压测瓶颈排查-网络带宽
  9. vue 之 双向绑定原理
  10. [Comet OJ - Contest #9 &amp; X Round 3] Namid[A]me