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
}
}
查看全文
相关阅读:
程序员的双11:3723亿,你贡献了几分力?
程序员国企1周上班5小时?国企VS私企,应该如何选择?
C++没有"垃圾回收"功能?智能指针,实现C++的内存管理
【C++学习笔记】什么是C++STL?掌握C++ STL的核心很重要!
都要2021年了,现代C++有什么值得我们学习的?
解析:C++如何实现简单的学生管理系统(源码分享)
程序员过高工资导致加班?应该降低程序员工资?网友:放过其他苦逼的程序员吧
结对-结对编程项目作业名称-需求分析
使用对话框模板创建一个InputBox()在C + +
选择图标
原文地址:https://www.cnblogs.com/flashicp/p/697929.html
最新文章
数组内存图
IDE常用快捷键
数组的遍历
数组空指针异常
数组的静态初始化
数组的动态初始化
数组的定义
Random
Continue和Break
死循环
热门文章
IPython Notebook
OSGi
Groovy -- Processing XML
Groovy User Guide
http://groovy.codehaus.org/
http://www.gradle.org/
Python v2.7.5
C++基础总结(二):C++基本数据类型及流控制语句详解
不知道想学哪种编程语言?这份“语言简历表”要收好
C++基础总结(一):从“hello world”入门C++!
Copyright © 2011-2022 走看看