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
;
}
}
}
}
}
查看全文
相关阅读:
CEPH篇 目录
kubernetes篇 容器用户权限控制
深度学习篇-如何理解置信区间
Mac软件安装篇 for Mac
JAVA进阶篇 内存模型
负载均衡篇 不同层次的负载均衡(2/3/4/7)
NETTY篇 一篇文章看懂NETTY
base64加密后无法解密
Spring-Cloud-Gateway 从升级到放弃
spring gateway 截取response 长度缺失
原文地址:https://www.cnblogs.com/flashicp/p/697892.html
最新文章
几个好用串口调试、检测的工具
如何解决mdk中,使用JLink提示错误信息read:@0x02000004
《c陷阱与缺陷》中的笔记——c语言null指针 && 宏定义的副作用
《c陷阱与缺陷》中的笔记——c语言数据转换的“坑”&& c语言无符号整数溢出
《c陷阱与缺陷》中的笔记——c语言求值顺序
《c陷阱与缺陷》中的笔记——c语言边界处理
《c陷阱与缺陷》——数组与指针的关系(一维数组,二维数组)
keil更新到5.30了,带来了比较多的新的特性,但开发老的项目不受影响
keil c51和keil mdk的共存方式
ubuntu设置简单密码
热门文章
sql自动生成golang结构体struct实体类
大战设计模式(第二季)【7】———— 从源码看桥接模式
大战设计模式(第二季)【6】———— 从源码看享元模式
大战设计模式(第二季)【5】———— 从源码看适配器模式
大战设计模式(第二季)【4】———— 从源码看装饰者模式
大战设计模式(第二季)【3】———— 从源码看原型模式
大战设计模式【23】—— 原型模式
大战设计模式(第二季)【2】———— 从源码看单例模式
大战设计模式(第二季)【1】———— 从源码看工厂模式
高性能消息队列NSQ
Copyright © 2011-2022 走看看