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
;
}
}
}
}
}
查看全文
相关阅读:
浅析阿里云API网关的产品架构和常见应用场景
30分钟全方位了解阿里云Elasticsearch(附公开课完整视频)
阿里云葛岱斌:让天下没有难做的安全运维
TPCx-BB官宣最新世界纪录,阿里巴巴计算力持续突破
从零开始入门 K8s | Kubernetes 网络概念及策略控制
云栖深度干货 | 打造“云边一体化”,时序时空数据库TSDB技术原理深度解密
AnalyticDB for PostgreSQL 6.0 新特性介绍
从零开始入门 K8s | 应用存储和持久化数据卷:核心知识
K8s 从懵圈到熟练 – 集群网络详解
理解SqlMapConfig.xml文件
原文地址:https://www.cnblogs.com/flashicp/p/697892.html
最新文章
ABP入门教程8
centos8上安装openresty
openresty使用redis作本地缓存
自己编写 Oracle 分页函数
oracle 行转列和列转行
(三)使用Jmeter模拟300个用户登录
数据库自动备份脚本并删除前3天的备份
字符串拆分函数 func_splitstr
(二)Jmeter各部件的作用
(一)Jmeter 简单的录制
热门文章
软件测试的发展阶段
Oracle 正则表达式
APP端测试与web端测试的区别
OpenCV视觉处理核心课程
《Python数据科学手册》
《OpenCV图像处理编程实例》
傅里叶变换—庖丁解牛
SIFT算法相关资料
RobHess的SIFT代码解析之kd树
阿里小程序Serverless 操作指南
Copyright © 2011-2022 走看看