zoukankan
html css js c++ java
GetData.cs
另一种Access数据库操作类
using
System;
using
System.Data;
using
System.Data.OleDb;
using
System.Text;
namespace
###
3
{
/**/
///
<summary>
///
GetData Access数据库。
///
</summary>
///
public
class
GetData
{
//
private string connString =" Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =";
//
连接字符串
public
OleDbConnection conn
=
null
;
/*属性*/
#region
/*属性*/
/**/
///
<summary>
///
连接数据库字符串
///
</summary>
private
string
p_connString
=
string
.Empty;
public
string
ConnString
{
get
{
return
p_connString;
}
set
{
p_connString
=
value;
}
}
/**/
///
<summary>
///
表名
///
</summary>
private
string
tableName
=
"
****
"
;
public
string
TableName
{
get
{
return
tableName;
}
set
{
tableName
=
value;
}
}
#endregion
/**/
///
<summary>
///
构造函数
///
</summary>
public
GetData()
{
OleDbConnection conn
=
new
OleDbConnection();
}
/***DbConnet***/
#region
/***DbConnet***/
/**/
///
<summary>
///
定义数据连接的字符串
///
</summary>
///
<returns>
OleDbConnection
</returns>
private
OleDbConnection ConnectToAccess()
{
OleDbConnection conn
=
new
OleDbConnection();
conn.ConnectionString
=
this
.p_connString;
try
{
conn.Open();
}
catch
(Exception ex)
{
throw
ex;
}
return
conn;
}
#endregion
/***查询数据***/
#region
/***查询数据***/
/**/
///
<summary>
///
查询数据
///
</summary>
///
<param name="operInt">
操作查询语句的标示
</param>
///
<returns>
DataSet查询结果
</returns>
public
DataSet GetTreeDataSet(
int
operInt)
{
OleDbDataAdapter da
=
null
;
string
selectSQL
=
getStrByOperInt(operInt);
if
(selectSQL
==
""
)
{
return
null
;
}
conn
=
ConnectToAccess();
da
=
new
OleDbDataAdapter(selectSQL,conn);
DataSet ds
=
new
DataSet();
try
{
da.Fill(ds);
da.Dispose();
}
catch
(Exception ex)
{
throw
new
Exception(ex.Message);
}
finally
{
if
(conn.State
!=
ConnectionState.Closed)
conn.Close();
conn.Dispose();
}
return
ds;
}
/**/
///
<summary>
///
构造查询字符串
///
</summary>
///
<param name="operInt">
操作查询语句的标示
</param>
///
<returns>
string
</returns>
private
string
getStrByOperInt(
int
operInt)
{
string
sqlStr
=
string
.Empty;
try
{
switch
(operInt)
{
case
1
:
sqlStr
=
Get
*
DataStr();
break
;
case
2
:
sqlStr
=
Get1DataStr();
break
;
case
3
:
sqlStr
=
Get2DataStr();
break
;
}
}
catch
(Exception err)
{
throw
new
Exception(err.Message);
}
return
sqlStr;
}
/***清除相同***/
#region
/***清除相同***/
/**/
///
<summary>
///
清除StatckInfo表格中的相同记录
///
</summary>
///
<returns>
是否成功清空
</returns>
public
bool
DeleteSameStatckInfoData()
{
int
rowCount
=-
1
;
try
{
string
strDelete
=
DeleteSameDataStr();
conn
=
ConnectToAccess();
OleDbCommand deleteCmd
=
new
OleDbCommand(strDelete.ToString(),conn);
rowCount
=
deleteCmd.ExecuteNonQuery();
}
catch
(Exception ex)
{
throw
new
Exception(ex.Message);
}
finally
{
if
(conn.State
!=
ConnectionState.Closed)
conn.Close();
conn.Dispose();
}
if
(rowCount
==-
1
||
rowCount
==
0
)
{
return
false
;
}
else
{
return
true
;
}
}
/**/
///
<summary>
///
删除##表中部分相同数据
///
</summary>
///
<returns>
查询字符串
</returns>
private
string
DeleteSameDataStr()
{
StringBuilder strDelete
=
new
StringBuilder();
strDelete.Append(
"
DELETE *
"
);
strDelete.Append(
"
FROM ##
"
);
strDelete.Append(
"
WHERE id NOT IN (
"
);
strDelete.Append(
"
SELECT MAX(id) FROM ##
"
);
strDelete.Append(
"
GROUP BY name,card,Fundsum,ID)
"
);
return
strDelete.ToString();
}
#endregion
/***存在验证***/
#region
/***存在验证***/
/**/
///
<summary>
///
某记录是否存在的查询
///
</summary>
///
<param name="statckinfo">
一条记录
</param>
///
<returns>
是否存在
</returns>
public
bool
GetDataExitsOrNot(StatckInfo statckinfo)
{
int
rowCount
=-
1
;
try
{
StringBuilder strSelect
=
new
StringBuilder();
strSelect.Append(
"
SELECT count(*) as co FROM #
"
);
strSelect.Append(
"
'
"
);
conn
=
ConnectToAccess();
OleDbCommand selectCmd
=
new
OleDbCommand(strSelect.ToString(),conn);
OleDbDataReader dr
=
selectCmd.ExecuteReader();
if
(dr.Read())
{
if
(
!
(dr[
"
co
"
]
is
System.DBNull))
rowCount
=
Int32.Parse(dr[
"
co
"
].ToString());
}
}
catch
(Exception ex)
{
throw
new
Exception(ex.Message);
}
finally
{
if
(conn.State
!=
ConnectionState.Closed)
conn.Close();
conn.Dispose();
}
if
(rowCount
==-
1
||
rowCount
==
0
)
{
return
false
;
}
else
{
return
true
;
}
}
#endregion
/***合并字符***/
#region
/***合并字符***/
/**/
///
<summary>
///
不同sql语句查询数据库,填充DataSet
///
</summary>
///
<param name="selectSQL">
string
</param>
///
<returns>
DataSet
</returns>
public
DataSet GetOtherDataByIDreceipt(
string
selectSQL)
{
if
(selectSQL
==
""
)
{
return
null
;
}
conn
=
ConnectToAccess();
OleDbDataAdapter da
=
new
OleDbDataAdapter(selectSQL,conn);
DataSet ds
=
new
DataSet();
try
{
da.Fill(ds);
da.Dispose();
}
catch
(Exception ex)
{
throw
new
Exception(ex.Message);
}
finally
{
if
(conn.State
!=
ConnectionState.Closed)
conn.Close();
conn.Dispose();
}
return
ds;
}
/**/
///
<summary>
///
循环DataSet,用","将所有ID合并成“,”间隔的字符串
///
</summary>
///
<param name="ds">
DataSet
</param>
///
<returns>
合并后的字符串
</returns>
private
string
getStrByDatset(DataSet ds)
{
string
characterStr
=
string
.Empty;
if
(ds
==
null
)
{
return
""
;
}
try
{
DataTable myTable
=
ds.Tables[
0
];
int
count
=
myTable.Rows.Count;
for
(
int
i
=
0
;i
<
myTable.Rows.Count;i
++
)
{
string
name
=
myTable.Rows[i][
0
].ToString();
characterStr
+=
name
+
"
,
"
;
}
}
catch
(Exception ex)
{
throw
new
Exception(ex.Message);
}
return
characterStr;
}
#endregion
/***清空表格***/
#region
/***清空表格***/
/**/
///
<summary>
///
清空##表格
///
</summary>
///
<returns>
是否成功清空
</returns>
public
bool
DeleteAllStatck()
{
int
rowCount
=-
1
;
try
{
string
strDelete
=
"
delete * FROM #·#
"
;
conn
=
ConnectToAccess();
OleDbCommand deleteCmd
=
new
OleDbCommand(strDelete.ToString(),conn);
rowCount
=
deleteCmd.ExecuteNonQuery();
}
catch
(Exception ex)
{
throw
new
Exception(ex.Message);
}
finally
{
if
(conn.State
!=
ConnectionState.Closed)
conn.Close();
conn.Dispose();
}
if
(rowCount
==-
1
||
rowCount
==
0
)
{
return
false
;
}
else
{
return
true
;
}
}
#endregion
}
}
查看全文
相关阅读:
抛开BlazeDS,自定义flex RPC
设计模式学习03装饰器模式
通过ANT生成MANIFEST.MF中的ClassPath属性
Spring JDBCTemplate与Hiberante混用
关于 两个 datetime 列的差别导致了运行时溢出
在Wcf中使用Nhibernate (续)
sql2005/sql2008 分页
工行支付api查询asp.net C# 实现
生成静态页面的vbscript
Asp.net Mvc Post ID Bug
原文地址:https://www.cnblogs.com/flashicp/p/697929.html
最新文章
SFTP 例子
SyncToy 两台Windows电脑文件同步
Windows ForFiles 删除 过期 文件
Nginx 配置注释
Windows 任务计划 定时重复执行程序
SQL Server 通过作业 自动按时 备份数据库
一些有用的Link
jcarousellite在jquery 1.2下出错的问题
JQuery中使用.CSS添加多个样式中出错
一段很有用的Alert代码
热门文章
Windows 2003下安装Adobe Creative Suite 3 Design Premium
访问网上邻居选择了"记住密码",取消"记住密码"的解决办法
C# 读取计算机CPU,HDD信息
网站常用色彩表(网络搜集整理)
我又回来了
设计模式学习02观察者模式
BlazeDS开发指南中文版第一章(开始BlazeDS试用之旅)
策略型网页游戏架构(flex+java)
设计模式学习01策略模式
设计模式学习04工厂方法模式
Copyright © 2011-2022 走看看