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
;
}
}
}
}
}
查看全文
相关阅读:
洛谷 P2234 [HNOI2002]营业额统计
洛谷p3146&p3147
洛谷 p1439 最长公共子序列
搜索
一步一步分析Caliburn.Micro(二:绑定执行方法Message现学现卖之自定命令)
一步一步分析Caliburn.Micro(一:绑定执行方法Message)
整理的C# 字符串类
不用ADOX.CatalogClass创建Access数据库文件
取远程网页数据 WebClient,HttpWebRequest
C# LinQ 与 ADO.NET
原文地址:https://www.cnblogs.com/flashicp/p/697892.html
最新文章
hadoop系列整理Hadoop
hadoop系列整理Kafka
hadoop系列整理Hive
Compressor For Mac 4.5.4 视频编辑软件
OmniGraffle Pro 7 For Mac 7.18.5 流程图绘制工具
Alfred 4 For Mac 4.6 无需鼠标的神器
OmniPlan 3 Pro for Mac 3.14.4 项目管理工具
BusyCal for Mac 2021.4.2 具有多个同步选项的强大日历应用程序。
Renamer for Mac 6.0.6 批量重命名文件。
Typinator for Mac 8.10.1 快速可靠的文本扩展解决方案。
热门文章
iThoughtsX for Mac 5.29 iOS 和 OS X 上的思维导图实用程序。
HandBrake for Mac 1.4.2 多功能视频编码器
Office for Mac 2019 微软的办公软件集合
C++程序中调用WebService的实现
单链表的逆置普通循环方法(详细图解)
关于位运算符的运用和理解
学习笔记考勤管理软件(一)
P4408 [NOI2003]逃学的小孩
洛谷p1020 导弹拦截
分块————优雅的暴力
Copyright © 2011-2022 走看看