zoukankan
html css js c++ java
DatabaseOp.cs
Access操作类
using
System;
using
System.Data;
using
System.Data.OleDb;
namespace
####
{
/**/
///
<summary>
///
后台数据库操作部分
///
</summary>
public
class
DatabaseOp
{
private
OleDbConnection conn
=
null
;
//
string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = C:\\Program Files\\##.mdb " ;
private
string
connString
=
"
Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =
"
;
//
连接字符串
public
DatabaseOp(
string
mdbFullName)
{
connString
+=
mdbFullName;
//
string path=Application.StartupPath + "\\Database\\";
//
connString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
//
+ path + "###.dat;User Id= ;Password= ;Jet OLEDB:System Database="
//
+ path + "——.dat;";
}
public
void
Close()
{
try
{
if
(conn
!=
null
)
{
conn.Close();
}
}
catch
{
conn
=
null
;
}
}
/**/
///
<summary>
///
返回数据库的阅读类
///
</summary>
///
<param name="strSql"></param>
///
<returns></returns>
public
OleDbDataReader GetDataReader(
string
strSql)
{
OleDbDataReader dbReader
=
null
;
if
(strSql
==
""
)
{
return
dbReader;
}
try
{
conn
=
new
OleDbConnection(
this
.connString);
if
(conn.State
==
ConnectionState.Open)
{
//
conn.Close();
//
int a=0;
}
else
{
conn.Open();
}
//
conn.Open();
OleDbCommand myCmd
=
conn.CreateCommand();
myCmd.CommandText
=
strSql;
dbReader
=
myCmd.ExecuteReader();
return
dbReader;
}
catch
(Exception err)
{
throw
err;
}
finally
{
//
if (dbReader!=null)
//
{
//
dbReader.Close();
//
dbReader=null;
//
}
//
if (conn!=null)
//
{
//
conn.Close();
//
conn=null;
//
}
}
}
public
object
GetFirstValue(
string
strSql)
{
object
obj
=
null
;
OleDbDataReader dbReader
=
null
;
if
(strSql
==
""
)
{
return
""
;
}
try
{
conn
=
new
OleDbConnection(
this
.connString);
conn.Open();
OleDbCommand myCmd
=
conn.CreateCommand();
myCmd.CommandText
=
strSql;
obj
=
myCmd.ExecuteScalar();
return
obj;
}
catch
(Exception err)
{
throw
err;
}
finally
{
if
(dbReader
!=
null
)
{
dbReader.Close();
dbReader
=
null
;
}
if
(conn
!=
null
)
{
conn.Close();
conn
=
null
;
}
}
}
public
bool
HasRows(
string
strSql)
{
bool
hasRow
=
false
;
OleDbDataReader dbReader
=
null
;
if
(strSql
==
""
)
{
return
hasRow;
}
try
{
conn
=
new
OleDbConnection(
this
.connString);
conn.Open();
OleDbCommand myCmd
=
conn.CreateCommand();
myCmd.CommandText
=
strSql;
dbReader
=
myCmd.ExecuteReader();
if
(dbReader.Read())
{
hasRow
=
true
;
}
return
hasRow;
}
catch
(Exception err)
{
throw
err;
}
finally
{
if
(dbReader
!=
null
)
{
dbReader.Close();
dbReader
=
null
;
}
if
(conn
!=
null
)
{
conn.Close();
conn
=
null
;
}
}
}
/**/
///
<summary>
///
执行Sql语句,操作数据库
///
</summary>
///
<param name="strSql"></param>
public
int
Execute(
string
strSql)
{
int
rows
=-
1
;
OleDbConnection conn
=
null
;
try
{
conn
=
new
OleDbConnection(
this
.connString);
conn.Open();
OleDbCommand nqcmd
=
conn.CreateCommand();
nqcmd.CommandText
=
strSql;
rows
=
nqcmd.ExecuteNonQuery();
return
rows;
}
catch
(Exception err)
{
throw
err;
}
finally
{
if
(conn
!=
null
)
{
conn.Close();
conn
=
null
;
}
}
}
/**/
///
<summary>
///
返回数据库的DataSet类
///
</summary>
///
<param name="strSql"></param>
///
<returns></returns>
public
DataSet GetDataSet(
string
strSql)
{
OleDbConnection conn
=
null
;
DataSet myDt
=
null
;
try
{
conn
=
new
OleDbConnection(
this
.connString);
conn.Open();
myDt
=
new
DataSet();
OleDbDataAdapter dataAdapter
=
new
OleDbDataAdapter(strSql,conn);
dataAdapter.Fill(myDt);
return
myDt;
}
catch
(Exception err)
{
throw
err;
}
finally
{
if
(conn
!=
null
)
{
conn.Close();
conn
=
null
;
}
}
}
}
}
查看全文
相关阅读:
jQuery瀑布流绝对定位布局(二)(延迟AJAX加载图片)
jQuery图片上传裁剪插件imgAreaSelect(分析四) 上传服务器端
jQuery表格的排序,
jQuery图片上传裁剪插件imgAreaSelect(分析二) 同步显示图像位置信息
jQuery图片上传裁剪插件imgAreaSelect(分析三) 如何获得选择域的图像信息
JS Get URL param
Dictionary 比List占用更多的内存
修改金蝶采购订单的默认采购方式
sqlserver 数据库操作记录 实现
用Python模拟键盘输入
原文地址:https://www.cnblogs.com/flashicp/p/697892.html
最新文章
不错的东东
gridview使用技巧(转)
sql存储过程删除无限极分类
asp.net 如何写入xml文件
验证码
C#3.0笔记(一)预备知识之Delegate
C#3.0笔记(二)预备知识之委托与事件
用Adapter模式重构以前系统的登录权限验证
C#3.0笔记(四)对象初始化器与匿名类型
C#3.0笔记(三)var关键字与扩展方法
热门文章
WCF学习(六)实例
WCF学习(五)数据契约之已知类型
程序员眼中的女人
发布一个比较粗糙的控件UpdownPanel
企业级开发基础之MSMQ
javascript鼠标拖动
jQuery拖动层(在可视区域范围内)
jQuery图片上传裁剪插件imgAreaSelect(分析一) 放大缩小
jQuery瀑布流浮动布局(一)(延迟AJAX加载图片)
jQuery菜单随滚条改为以定位方式
Copyright © 2011-2022 走看看