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
;
}
}
}
}
}
查看全文
相关阅读:
软件项目版本号的命名规则及格式
你必须知道的C#的25个基础概念
Visual C#常用函数和方法集汇总
web标准下的web开发流程思考
设计模式(5)>模板方法 小强斋
设计模式(9)>迭代器模式 小强斋
设计模式(10)>策略模式 小强斋
设计模式(8)>代理模式 小强斋
设计模式(7)>观察者模式 小强斋
设计模式(7)>观察者模式 小强斋
原文地址:https://www.cnblogs.com/flashicp/p/697892.html
最新文章
PHP性能分析(摘要)
Tornado官网被墙,复制一份
FaceBook的Link Button添加方式
jQuery UI官网所使用的AJAX页面转换,代码分析
批量生成term的测试数据
使用UI Automation实现自动化测试4.9 (ScrollPattern)
使用UI Automation实现自动化测试6 (模拟鼠标在自动化测试中的应用)
使用UI Automation实现自动化测试5 (Winfrom和WPF中弹出和关闭对话框的不同处理方式)
Trigger study
The Dot Net Application Domain Study
热门文章
使用UI Automation实现自动化测试4.8 (GridPattern)
使用UI Automation实现自动化测试7.1 (模拟键盘输入数据在自动化测试中的应用)
通过javascript来控制ASP.NET服务器控件状态
使用UI Automation实现自动化测试4.7 (TogglePattern)
使用UI Automation实现自动化测试4.6.2 (SelectItemPattern Demo)
pascal 实现排序法
Delphi2007 让我心动
界面设计规范
ASP.NET程序中常用的三十三种代码
关于“args”?
Copyright © 2011-2022 走看看