using System;
using System.Collections;
using System.Data;
using System.Globalization;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;
namespace com.joybase.DB
{
///
1<summary>
2/// <b>程序集名称:com.joybase.DB.dll(开发版)</b>
3
4/// 版本号:1.4.0.0(for VS.NET正式版);
5
6/// 开发人员::开心就好
7
8/// </summary>
///
1<remarks>
2/// <b>电子邮件:</b>[email protected]
3
4/// <b>网站:</b>http://www.joycode.com(2001/12/1开通)
5
6/// 功能列表
7/// 本程序集由<font color="red"><b>开心就好</b></font>开发,如果您在使用中遇到任何疑问,可以<a href=" mailto:[email protected] ">致信反馈</a>,十分感谢,请在信件中给出使用的代码片段及出错的详细描述!
8
9/// 数据库操作类,效果有:
10
11/// 1.可以跨越任何ADO.NET支持的数据库;(已经支持,经过测试的数据有MS Access以及MS SQLServer2000,以及Oracle8i系列)
12
13/// 2.可以执行存储过程及普通SQL语句;(已经支持)
14
15/// 3.可以得到存储过程返回的值及出口参数;(已经支持)
16
17/// 4.简单的可以达到分页效果;(DataSet已经支持,DataReader已支持)
18
19/// 5.可以以DataSet、DataReader、DataTable以及无结果输出;
20
21/// 6.事务批处理功能(暂不支持)
22
23/// 7.日后将支持System.Data.ODBC驱动(暂不支持)
24
25/// <div align="center"><b>更新列表(1.3.0.1)</b></div>
26/// 1.增加了一个JoyBaseDBException异常类,用户可以自己捕捉异常
27
28/// 2.JoyBaseDBException异常类的Reason属性可以查阅当前异常的错误原因
29
30/// 3.更新了部分示例
31
32/// 4.本升级是由“蓝”请求而特意打造,在此表示一并感谢;
33
34/// <div align="center"><b>更新列表(1.3.0.0)</b></div>
35
36/// 1.修改了原来的构造方法,加进了无参构造,去除了原来的单参构造方法;
37
38/// 2.新增加两个属性,即ConnectionSetName,相当于原来的构造方法中的单一入参,即在Config文件中配置连接字符串的键名,以及ConnectionString,可以直接赋入数据库连接字符串,如果此键赋入,则将覆盖原来的ConnectionStringSetName;
39
40/// 3.修改了原来的ExecuteDataTable的Bug;
41
42/// 4.修改了上一版本的两个分页方法,将out型参数提取为属性;
43
44/// 5.内部的一些其它小型Bug
45/// <div align="center"><b>更新列表(1.2.0.3)</b></div>
46
47/// 1.去除了原来的Execute方法,将其执行分解为多个方法;
48
49/// 2.去除了ReturnType属性以及ResultType枚举类型。
50
51/// 3.增加了ExecuteNoresult方法,返回值为纪录影响数;
52
53/// 4.增加了ExecuteDataTable方法,返回一个System.Data.DataTable,该表的名称默认为“Table”
54
55/// 5.增加了ExecuteDataReader方法,返回一个System.Data.IDataReader对象
56
57/// 6.重载ExecuteDataReader方法,支持分页显示,参数列表为
58
59/// ExecuteDataReader(int PageSize,int CurrentPage,out int PageCount,out int PageSize)
60
61/// PageSize:每页显示的纪录数;
62
63/// CurrentPage;需要返回的页面号;
64
65/// PageCount:回参,返回页面总数;
66
67/// PageSize:回参,返回纪录总数;
68
69/// 7.增加了ExecuteDataSet方法,并且有四次重载,分别是
70
71/// (1)ExecuteDataSet()
72
73/// 返回一个System.Data.DataSet
74
75/// (2)ExecuteDataSet(string TableName);
76
77/// 入参为表的名称;
78
79/// (3)ExecuteDataSet(string Tablename,int StartRecord,int MaxRecord);
80
81/// 返回从第StartRecord条纪录开始数的总共MaxRecord条纪录。
82
83/// (4)ExecuteDataSet(string TableName,int PageSize,int CurrentPage,out int PageCount,out int RecordCount)
84
85/// 请参阅关于DataReader分页方法的叙述。
86
87///
88/// </remarks>
///
1<example>
2///
3/// 注意:在运行示例之前,您必须满足以下条件:<ol><li>请先在您的配置文件(如果是ASP.NET程序为Web.Config,如果是Win Form程序,则为App.Config文件,加上以下一句话:
4/// <appSettings>
5
6/// <add key="DSN" value="server=(local)\NetSDK;database=Northwind;Trusted_Connection=yes" />
7
8/// </appSettings\>
9
10/// 请注意,这句话必须加在配置文件的根节点,即<configuration>之下</li>
11/// <li>您已经安装了微软的MSDE数据库,如果您没有安装,可以在您的VS.NET安装目录下查找到,如我安装在E盘,则MSDE的安装程序为E:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Samples\Setup\msde\instmsde.exe,另外,请保证您的MSDE已经启动,可以在命令行使用net start MSSQL$NetSDK 将其进行启动</li>
12/// <li>以下示例仅是给出了MS SQL Server的桌面数据库的示例,在您有条件的情况下,您可以使用其它数据库来做试验,如果有任何问题,可以与我联系</li>
13/// </ol>
14/// <b>例一:执行查询操作,以DataReader返回结果集</b>
15
16/// <code>
17/// //请注意,此处的"DSN"为您在配置文件中所给的键名,而不是值;
18
19/// try
20
21/// {
22
23/// Command command=new Command();
24
25/// command.ConnectionString="server=(local)\NetSDK;database=Northwind;Trusted_Connection=yes";
26
27/// command.CommandText="select * from orders where orderid=@orderid";<br/>
28/// //下面的orders对应于SQL语句中的字段名,同时,如果查询语句为存储过程,此处请插入相应的入参;
29
30/// command.Parameters["orderid"]=10249;
31
32/// System.Data.IDataReader dr=command.ExecuteDataReader();
33
34/// }
35
36/// catch(JoyBaseDBException e)
37
38/// {
39
40/// //根据下面的示例你可以自己制作自己的异常捕捉;
41
42/// Console.WriteLine("错误信息为:"+e.Message);
43
44/// Console.WriteLine("错误可能原因为:"+e.Reason);
45
46/// }
47
48///
49///if(dr.read())
50
51///{
52
53/// //读取数据,按照您原来对DataReader的操作方法即可;
54
55/// ...
56
57/// }
58
59/// else
60
61/// {
62
63/// //执行您的异常操作;
64
65/// ...
66
67///
68
69/// }
70
71/// dr.Close();
72
73/// </code>
74/// <b>例二:执行查询操作,并且返回DataSet对象:</b>
75
76/// <code>
77/// Command command=new Command();
78
79/// command.ConnectionSetName="DSN";
80
81/// command.CommandText="select * from orders";
82
83/// //下面我们分别返回几个DataSet对象
84
85/// //第一种是带默认表名为DataSet为“Table”的DataSet
86
87/// System.Data.DataSet ds1=command.ExecuteDataSet();
88
89/// //然后再返回一个自定义表名为“TableName”的对象
90
91/// System.Data.DataSet ds2=command.ExecuteDataSet("TableName");
92
93/// //再返回一个DataSet,我们将限制结果集的返回,假设从第12个纪录开始,查询50个纪录出来
94
95/// System.Data.DataSet ds3=command.ExecuteDataSet("TableName",12,50);
96
97/// //同时,你如果乐意,还可以只返回一个DataTable
98
99/// System.Data.DataTable table=command.Execute.DataTable();
100
101/// //现在您可以执行数据绑定操作了,直接取它的defaultView即可
102
103/// ...
104
105/// </code>
106/// <b>例三:执行分页查询,并且返回DataSet对象以及DataReader对象</b>
107
108/// <code>
109/// Command command=new Command();
110
111/// command.ConnectionSetName="DSN";
112
113/// command.CommandText="select * from orders";
114
115/// command.PageSize=30;
116
117/// //此处您可以用变量替代,即可以达到动态分页的效果
118
119/// int CurrentPage=1;
120
121/// System.Data.DataSet ds=command.ExecuteDataSet("ordersTable",CurrentPage);
122
123/// //执行绑定操作,这样DataList以及其它数据绑定控件均支持分页了(您还得做一些操作,如当前页面数不要大于页面总数也不要小于等于0等的判断)
124
125/// ...
126
127/// //下面输出DataReader
128
129/// System.Data.IDataReader dr=command.ExecuteDataReader(CurrentPage);
130
131/// int i=0;
132
133/// while(dr.read())
134
135/// {
136
137/// //执行您需要的操作
138
139/// ....
140
141///
142/// }
143
144/// string showText="共有纪录"+command.RecordSet+"条,共有"+command.PageCount+"页,当前显示第"+CurrentPage+"页";
145
146///
147
148/// </code>
149/// </example>
public class Command
{
//private DBParameters m_Parameters;
///
1<summary>
2/// 数据库命令,System.Data.IDbCommand接口类型
3/// </summary>
private System.Data.IDbCommand m_command;
///
1<summary>
2/// 内部参数,每页的纪录数
3/// </summary>
private int m_PageSize;
///
1<summary>
2/// 总共的页数
3/// </summary>
private int m_PageCount;
///
1<summary>
2/// 总共的纪录数
3/// </summary>
private int m_RecordCount;
///
1<summary>
2/// SQL语句及存储过程的命令文本,字符串类型
3/// </summary>
private string m_CommandText;
///
1<summary>
2/// 命令参数集合
3/// </summary>
private System.Collections.Hashtable m_Parameter;
///
1<summary>
2/// 只写属性,将连接字符串在Config文件中的键名赋值进来
3/// </summary>
[Category("(Data Binding)"),Description("数据库连接字符串在Config文件中的键名")]
public string ConnectionSetName
{
set
{
Provider.ConnectionSetName=value;
}
}
///
1<summary>
2/// 只读属性,返回总共的页数
3/// </summary>
[Browsable(false)]
public int PageCount
{
get
{
return this.m_PageCount;
}
}
///
1<summary>
2/// 只写属性,设置每页的纪录数
3/// </summary>
[Category("Pager Info"),Description("每页显示的纪录数")]
public int PageSize
{
set
{
this.m_PageSize=value;
}
}
///
1<summary>
2/// 只读属性,获得总共的纪录数
3/// </summary>
[Browsable(false)]
public int RecordCount
{
get
{
return this.m_RecordCount;
}
}
///
1<summary>
2/// 构造方法
3/// </summary>
public Command()
{
this.m_PageCount=0;
this.m_PageSize=0;
this.m_RecordCount=0;
m_CommandText="";
m_Parameter=new System.Collections.Hashtable();
}
///
1<summary>
2/// 只写属性,连接字符串,注意,本属性可以覆盖ConnectionSetName属性的值
3/// </summary>
[Browsable(true),Category("(Data Binding)"),Description("设置数据库连接字符串"),Editor(typeof(FolderNameEditor), typeof(UITypeEditor))]
public string test
{
get
{
System.Resources.ResourceManager rm=new System.Resources.ResourceManager(typeof(Command));
return rm.GetString("hello");
}
}
public string ConnectionString
{
set
{
Provider.ConnectionString=value;
}
}
///
1<summary>
2/// 字符串类型,SQL语句及存储过程的命令文本,只读
3/// </summary>
///
1<remarks>
2/// SQL语句可以以入参方式表示,如"select * from user where username=@username "即为一个合法的命令文本,我们可以以参数形式动态为@username赋值
3/// </remarks>
[Category("(DataBinding"),Description("需要执行的SQL查询的文本以及存储过程的名称")]
public string CommandText
{
set
{
m_command=Provider.getConn().CreateCommand();
//this.m_Parameters=(DBParameters)this.m_command.Parameters;
this.m_CommandText=value;
}
}
///
1<summary>
2/// 设置当前的参数类型。
3/// </summary>
[Category("(Data Binding)"),Description("设置连接字符串")]
public System.Collections.Hashtable Parameter
{
set
{
this.m_Parameter=value;
}
get
{
return this.m_Parameter;
}
}
// ///
1<summary>
2// /// 暂不支持
3// /// </summary>
// public DBParameters Parameters
// {
// get
// {
// return this.m_Parameters;
//
//
// }
// set
// {
// this.m_Parameters=value;
// }
// }
//
///
1<summary>
2/// 得到出口参数的值,仅在命令文本为存储过程且设置了出口参数时有效;
3/// </summary>
public System.Data.IDataParameterCollection ReturnValue
{
get
{
return this.m_command.Parameters;
}
}
///
1<summary>
2/// 内部处理方法,不对外公开
3/// </summary>
private void Prepare()
{
//System.Threading.Thread.GetDomain().UnhandledException+=new UnhandledExceptionEventHandler(ThrowDBException);
try
{
//m_command=Provider.getConn().CreateCommand();
m_command.CommandText=this.m_CommandText;
System.Collections.IDictionaryEnumerator One=this.m_Parameter.GetEnumerator();
while(One.MoveNext())
{
System.Data.IDataParameter parameter=this.m_command.CreateParameter();
parameter.SourceVersion =System.Data.DataRowVersion.Current;
parameter.Value=One.Value;
parameter.ParameterName=(string)One.Key;
this.m_command.Parameters.Add(parameter);
}
this.m_command.Connection.Close();
this.m_command.Connection.Open();
//this.m_command.Prepare();
}
catch(Exception e)
{
string reason="(1)SQL语句或者存储过程使用不当,或者将特定数据库的检索语句使用到了不当的数据库上;\r\n(2)SQL语句或者存储过程的入参的的赋值错误,如将字符串赋给了int类型等\r\n";
throw new JoyBaseDBException(e.Message,reason);
}
}
///
1<summary>
2/// 执行一次更新或者插入操作
3/// </summary>
///
1<returns>返回该次操作所影响的纪录集数</returns>
public int ExecuteNoResult()
{
this.Prepare();
int result=0;
try
{
result=this.m_command.ExecuteNonQuery();
}
catch(Exception e)
{
throw new JoyBaseDBException(e.Message,e);
}
//this.m_Parameters.Clear();
return result;
}
///
1<summary>
2/// 执行查询操作,并且按照规定的分页形式返回DataReader结果集
3/// </summary>
///
1<param name="p_CurrentPageIndex"/>
需要返回的页面号
///
1<returns>返回一个System.Data.IDataReader方法</returns>
public System.Data.IDataReader ExecuteDataReader(int p_CurrentPageIndex)
{
System.Data.IDataReader result=null;
this.Prepare();
try
{
result=this.m_command.ExecuteReader();
System.Data.DataSet ds=this.ExecuteDataSet();
this.m_RecordCount=ds.Tables[0].Rows.Count;
//
ds.Clear();
ds.Dispose();
if(this.m_RecordCount%this.m_PageSize==0)
{
this.m_PageCount=this.m_RecordCount/this.m_PageSize;
}
else
{
this.m_PageCount=this.m_RecordCount/this.m_PageSize+1;
}
int StartCount=(p_CurrentPageIndex-1)*this.m_PageSize;
for(int i=0;i
1<startcount;i++) ;="" <summary="" catch(exception="" e)="" joybasedbexception(e.message,reason);="" new="" reason="(1)未设置PageSize变量或者将其设为不合理数值,而直接调用了分页方法\r\n;(2)检索的页号不存在或者溢出;\r\n" result.read();="" result;="" return="" string="" this.m_parameters.clear();="" throw="" {="" }="">
2/// 执行一个查询操作,并且以System.Data.IDataReader()来返回结果集
3///
4/// <returns>返回的System.Data.IDataReader</returns>
5public System.Data.IDataReader ExecuteDataReader()
6{
7
8System.Data.IDataReader result=null;
9this.Prepare();
10try
11{
12result=this.m_command.ExecuteReader();
13}
14catch(Exception e)
15{
16throw new JoyBaseDBException(e.Message,e);
17}
18//this.m_Parameters.Clear();
19return result;
20
21}
22/// <summary>
23/// 执行一个查询操作,并且返回一个DataTable
24/// </summary>
25/// <returns>返回一个DataTable</returns>
26public System.Data.DataTable ExecuteDataTable()
27{
28this.Prepare();
29System.Data.DataTable result=new System.Data.DataTable();
30//System.Data.Common.DbDataAdapter Adapter=null;
31try
32{
33if(Provider.Type==DBType.SqlClient)
34{
35System.Data.SqlClient.SqlDataAdapter Adapter=new System.Data.SqlClient.SqlDataAdapter((System.Data.SqlClient.SqlCommand)this.m_command);
36//Adapter.SelectCommand=new System.Data.SqlClient.SqlCommand(,(System.Data.SqlClient.SqlConnection)Provider.getConn());//(System.Data.SqlClient.SqlCommand)this.m_command;
37
38Adapter.Fill(result);
39
40
41}
42else
43{
44System.Data.OleDb.OleDbDataAdapter Adapter=new System.Data.OleDb.OleDbDataAdapter((System.Data.OleDb.OleDbCommand)this.m_command);
45//Adapter.SelectCommand=(System.Data.OleDb.OleDbCommand)this.m_command;
46Adapter.Fill(result);
47}
48}
49catch(Exception e)
50{
51throw new JoyBaseDBException(e.Message,e);
52}
53//this.m_Parameters.Clear();
54return result;
55
56
57}
58/// <summary>
59/// 返回分页的DataSet页面
60/// </summary>
61/// <param name="p_TableName"/>DataSet中的表名
62/// <param name="p_CurrentPageIndex"/>需要返回的页面
63/// <returns></returns>
64public System.Data.DataSet ExecuteDataSet(string p_TableName,int p_CurrentPageIndex)
65
66{
67System.Data.DataSet ds=this.ExecuteDataSet();
68try
69{
70
71
72this.m_RecordCount=ds.Tables[0].Rows.Count;
73//
74ds.Clear();
75ds.Dispose();
76if(this.m_RecordCount%this.m_PageSize==0)
77{
78this.m_PageCount=this.m_RecordCount/this.m_PageSize;
79
80}
81else
82{
83this.m_PageCount=this.m_RecordCount/this.m_PageSize+1;
84}
85int StartCount=(p_CurrentPageIndex-1)*this.m_PageSize;
86ds=this.ExecuteDataSet(p_TableName,StartCount,this.m_PageSize);
87}
88catch(Exception e)
89{
90string reason="(1)未设置PageSize变量或者将其设为不合理数值,而直接调用了分页方法\r\n;(2)检索的页号不存在或者溢出;\r\n";
91throw new JoyBaseDBException(e.Message,reason);
92}
93//this.m_Parameters.Clear();
94
95return ds;
96
97
98}
99/// <summary>
100/// 执行一个查询操作,并且返回一个DataSet对象,能够执行分页操作
101/// </summary>
102/// <param name="p_StartRecord"/>起始的记录号,以0开始
103/// <param name="p_MaxRecords"/>返回的最多的记录号
104/// <param name="p_TableName"/>返回的DataSet中包含的表的名称
105/// <returns>返回一个DataSet对象</returns>
106public System.Data.DataSet ExecuteDataSet(string p_TableName,int p_StartRecord,int p_MaxRecords)
107{
108this.Prepare();
109System.Data.DataSet result=new System.Data.DataSet();
110//System.Data.Common.DbDataAdapter Adapter=null;
111try
112{
113if(Provider.Type==DBType.SqlClient)
114{
115System.Data.SqlClient.SqlDataAdapter Adapter=new System.Data.SqlClient.SqlDataAdapter((System.Data.SqlClient.SqlCommand)this.m_command);
116//Adapter.SelectCommand=new System.Data.SqlClient.SqlCommand("select * from orders",(System.Data.SqlClient.SqlConnection)Provider.getConn("DSN"));//(System.Data.SqlClient.SqlCommand)this.m_command;
117
118Adapter.Fill(result,p_StartRecord,p_MaxRecords,p_TableName);
119
120
121}
122else
123{
124System.Data.OleDb.OleDbDataAdapter Adapter=new System.Data.OleDb.OleDbDataAdapter((System.Data.OleDb.OleDbCommand)this.m_command);
125//Adapter.SelectCommand=(System.Data.OleDb.OleDbCommand)this.m_command;
126Adapter.Fill(result,p_StartRecord,p_MaxRecords,p_TableName);
127}
128}
129catch(Exception e)
130{
131throw new JoyBaseDBException(e.Message,e);
132}
133//this.m_Parameters.Clear();
134return result;
135
136
137}
138/// <summary>
139/// 执行查询操作,并且返回一个DataSet对象
140/// </summary>
141/// <param name="p_TableName"/>DataSet中包含的表的名称
142/// <returns>返回一个DataSet对象</returns>
143public System.Data.DataSet ExecuteDataSet(string p_TableName)
144{
145this.Prepare();
146System.Data.DataSet result=new System.Data.DataSet();
147try
148{
149//System.Data.Common.DbDataAdapter Adapter=null;
150if(Provider.Type==DBType.SqlClient)
151{
152System.Data.SqlClient.SqlDataAdapter Adapter=new System.Data.SqlClient.SqlDataAdapter((System.Data.SqlClient.SqlCommand)this.m_command);
153//Adapter.SelectCommand=new System.Data.SqlClient.SqlCommand("select * from orders",(System.Data.SqlClient.SqlConnection)Provider.getConn("DSN"));//(System.Data.SqlClient.SqlCommand)this.m_command;
154
155Adapter.Fill(result,p_TableName);
156
157
158}
159else
160{
161System.Data.OleDb.OleDbDataAdapter Adapter=new System.Data.OleDb.OleDbDataAdapter((System.Data.OleDb.OleDbCommand)this.m_command);
162//Adapter.SelectCommand=(System.Data.OleDb.OleDbCommand)this.m_command;
163Adapter.Fill(result,p_TableName);
164}
165}
166catch(Exception e)
167{
168
169
170throw new JoyBaseDBException(e.Message,e);
171}
172//this.m_Parameters.Clear();
173return result;
174
175
176}
177/// <summary>
178/// 执行查询操作,并且返回一个DataSet对象
179/// </summary>
180/// <returns>返回一个DataSet对象,并且其中的DataSet中的表名称为默认的“Table”</returns>
181public System.Data.DataSet ExecuteDataSet()
182{
183this.Prepare();
184System.Data.DataSet result=new System.Data.DataSet();
185try
186{
187if(Provider.Type==DBType.SqlClient)
188{
189System.Data.SqlClient.SqlDataAdapter Adapter=new System.Data.SqlClient.SqlDataAdapter((System.Data.SqlClient.SqlCommand)this.m_command);
190
191
192Adapter.Fill(result);
193
194}
195else
196{
197System.Data.OleDb.OleDbDataAdapter Adapter=new System.Data.OleDb.OleDbDataAdapter((System.Data.OleDb.OleDbCommand)this.m_command);
198
199Adapter.Fill(result);
200}
201}
202catch(Exception e)
203{
204throw new JoyBaseDBException(e.Message,e);
205}
206//this.m_Parameters.Clear();
207return result;
208
209
210}
211
212
213/// <summary>
214/// 内部保护的数据库连接类
215/// </summary>
216protected abstract class Provider
217{
218//一个静态的连接接口;
219private static System.Data.IDbConnection conn;
220/// <summary>
221/// 构造方法,设为私有类型,是防止外部无效的引用;
222/// </summary>
223protected Provider()
224{
225System.Data.IDbCommand x=new System.Data.OleDb.OleDbCommand();
226}
227
228/// <summary>
229/// 返回数据库边接类型
230/// </summary>
231public static DBType Type
232{
233get
234{
235
236return Provider.m_DBType;
237}
238set
239{
240Provider.m_DBType=value;
241}
242}
243private static string m_ConnectionString;
244private static string m_ConnectionStringSetName;
245/// <summary>
246/// 数据库连接字符串设置名称
247/// </summary>
248public static string ConnectionSetName
249{
250set
251{
252Provider.m_ConnectionStringSetName=value;
253
254}
255}
256/// <summary>
257/// 数据库连接字符串
258/// </summary>
259public static string ConnectionString
260{
261set
262{
263Provider.m_ConnectionString=value;
264
265}
266}
267private static DBType m_DBType;
268
269/// <summary>
270/// 获得数据库连接接口
271/// </summary>
272/// <returns>返回一个连接</returns>
273public static System.Data.IDbConnection getConn()
274{
275string ConnStr="";
276try{
277//System.Threading.Thread.GetDomain().UnhandledException+=new UnhandledExceptionEventHandler(ThrowDBException);
278if(Provider.m_ConnectionString==""||Provider.m_ConnectionString==null)
279{
280if(Provider.m_ConnectionStringSetName.Trim()=="")
281{
282ConnStr=System.Configuration.ConfigurationSettings.AppSettings["DataBase.ConnectionString"];
283}
284else
285{
286ConnStr=System.Configuration.ConfigurationSettings.AppSettings[Provider.m_ConnectionStringSetName];
287}
288
289
290}
291else
292{
293ConnStr=Provider.m_ConnectionString;
294}
295if(ConnStr==null||ConnStr=="")
296{
297throw new JoyBaseDBException("连接字符串为空或者是null类型,请检查您的ConnectionString以及ConnectionSetName是否进行正确设置,或者阅读相关说明.");
298}
299//DBType m_DBType;//=Provider.DataBaseType;
300/*
301* 注释:我们对前面的编码进行了部分的修改,鉴于System.Data.SqlClient的连接
302* 字符串当中不可能出现"Provider"字样,所以我们根据是否有Provider字样来判断
303* 该连接是基于System.Data.SqlClient的或者System.Data.OleDB的。
304* 参考资料:
305* 可以将 ConnectionString 属性设置为单个单元。(不能为 SqlConnection 对象指定 Provider 属性。)
306* –或–
307*
308* 可以设置单个属性(DataSource、Database、UserName 等等)。如果设置单个属性,则将为您生成连接字符串。
309* 注意 在连接字符串中存储用户名和密码有安全性设置的意味。有关详细信息,请参阅Introduction to ADO.NET Connection Design Tools。
310*
311*/
312if(ConnStr.ToLower().IndexOf("provider")==-1) Provider.Type=DBType.SqlClient;
313else Provider.Type=DBType.OleDB;
314//throw new Exception("here");
315
316if(m_DBType==DBType.SqlClient)
317{
318conn=new System.Data.SqlClient.SqlConnection(ConnStr);
319}
320else
321{
322conn=new System.Data.OleDb.OleDbConnection(ConnStr);
323}
324}
325catch(Exception e)
326{
327string reason="(1)未设置数据库连接字符串,请重新检查连接字符串\r\n(2)目标数据库不存在,或者是没有启动数据库或者无法登录;\r\n(3)连接字符串设置不正确,请按照标准的连接方式来写。\r\n";
328throw new JoyBaseDBException(e.Message,reason);
329}
330return conn;
331}
332}
333}
334
335/// <summary>
336/// 枚举类型,即一个数据库连接类型的枚举
337/// </summary>
338public enum DBType
339{
340/// <summary>
341/// SQL方式连接
342/// </summary>
343SqlClient=0,
344/// <summary>
345/// OLEDB方式连接
346/// </summary>
347OleDB=1
348}
349/// <summary>
350/// 本程序集的异常信息
351/// </summary>
352public class JoyBaseDBException:Exception
353{
354/// <summary>
355/// 构造方法
356/// </summary>
357public JoyBaseDBException():base()
358{
359this.HelpLink=" http://www.joycode.com ";
360this._Reason="无特定原因";
361}
362/// <summary>
363/// 构造方法
364/// </summary>
365/// <param name="message"/>错误信息
366public JoyBaseDBException(string message):base("com.joybase.DB.dll Exception Message:"+message)
367{
368//this.Message=message;
369this._Reason="无特定原因";
370this.HelpLink=" http://www.joycode.com ";
371}
372/// <summary>
373/// 构造方法
374/// </summary>
375/// <param name="message"/>错误信息
376/// <param name="e"/>内部异常
377public JoyBaseDBException(string message,System.Exception e):base("com.joybase.DB.dll Exception Message:"+message,e)
378{
379//this.Message=;
380this.HelpLink=" http://www.joycode.com ";
381this._Reason="无特定原因";
382}
383/// <summary>
384/// 构造方法
385/// </summary>
386/// <param name="message"/>错误信息
387/// <param name="reason"/>错误原因
388public JoyBaseDBException(string message,string reason):base("com.joybase.DB.dll Exception Message:"+message+".更多信息请捕捉本异常的Reason变量")
389{
390
391this._Reason="可能原因如下:\r\n"+reason;
392}
393
394private string _Reason;
395/// <summary>
396/// 错误原因,只读
397/// </summary>
398public string Reason
399{
400get
401{
402return this._Reason;
403}
404}
405
406}
407// /// <summary>
408// ///
409// /// </summary>
410// public class DBParameter:IDataParameter
411// {
412// DbType m_dbType = DbType.Object;
413// ParameterDirection m_direction = ParameterDirection.Input;
414// bool m_fNullable = false;
415// string m_sParamName;
416// string m_sSourceColumn;
417// DataRowVersion m_sourceVersion = DataRowVersion.Current;
418// object m_value;
419//
420// public DBParameter()
421// {
422// }
423//
424// public DBParameter(string parameterName, DbType type)
425// {
426// m_sParamName = parameterName;
427// m_dbType = type;
428// }
429//
430// public DBParameter(string parameterName, object value)
431// {
432// m_sParamName = parameterName;
433// this.Value = value;
434// // Setting the value also infers the type.
435// }
436//
437// public DBParameter( string parameterName, DbType dbType, string sourceColumn )
438// {
439// m_sParamName = parameterName;
440// m_dbType = dbType;
441// m_sSourceColumn = sourceColumn;
442// }
443//
444// public DbType DbType
445// {
446// get { return m_dbType; }
447// set { m_dbType = value; }
448// }
449//
450// public ParameterDirection Direction
451// {
452// get { return m_direction; }
453// set { m_direction = value; }
454// }
455//
456// public Boolean IsNullable
457// {
458// get { return m_fNullable; }
459// }
460//
461// public String ParameterName
462// {
463// get { return m_sParamName; }
464// set { m_sParamName = value; }
465// }
466//
467// public String SourceColumn
468// {
469// get { return m_sSourceColumn; }
470// set { m_sSourceColumn = value; }
471// }
472//
473// public DataRowVersion SourceVersion
474// {
475// get { return m_sourceVersion; }
476// set { m_sourceVersion = value; }
477// }
478//
479// public object Value
480// {
481// get
482// {
483// return m_value;
484// }
485// set
486// {
487// m_value = value;
488// m_dbType = _inferType(value);
489// }
490// }
491//
492// private DbType _inferType(Object value)
493// {
494// switch (Type.GetTypeCode(value.GetType()))
495// {
496// case TypeCode.Empty:
497// throw new SystemException("Invalid data type");
498//
499// case TypeCode.Object:
500// return DbType.Object;
501//
502// case TypeCode.DBNull:
503// case TypeCode.Char:
504// case TypeCode.SByte:
505// case TypeCode.UInt16:
506// case TypeCode.UInt32:
507// case TypeCode.UInt64:
508// // Throw a SystemException for unsupported data types.
509// throw new SystemException("Invalid data type");
510//
511// case TypeCode.Boolean:
512// return DbType.Boolean;
513//
514// case TypeCode.Byte:
515// return DbType.Byte;
516//
517// case TypeCode.Int16:
518// return DbType.Int16;
519//
520// case TypeCode.Int32:
521// return DbType.Int32;
522//
523// case TypeCode.Int64:
524// return DbType.Int64;
525//
526// case TypeCode.Single:
527// return DbType.Single;
528//
529// case TypeCode.Double:
530// return DbType.Double;
531//
532// case TypeCode.Decimal:
533// return DbType.Decimal;
534//
535// case TypeCode.DateTime:
536// return DbType.DateTime;
537//
538// case TypeCode.String:
539// return DbType.String;
540//
541// default:
542// throw new SystemException("Value is of unknown data type");
543// }
544// }
545// }
546//
547// public class DBParameters: System.Collections.ArrayList,IDataParameterCollection
548// {
549// //private DBParameter x[int];
550//// public object this[string index]
551//// {
552//// get;set;
553//// }
554//
555// public object this[string index]
556// {
557// get
558// {
559//
560// return this[IndexOf(index)];
561// }
562// set
563// {
564// this[IndexOf(index)] = value;
565// }
566// }
567//
568//
569//// public DBParameter this[string x]
570//// {
571//// get
572//// {
573//// }
574//// set
575//// {
576//// }
577//// }
578//// public DBParameter this[string index]
579//// {
580//// get
581//// {
582//// return (DBParameter)this[IndexOf(index)];
583////
584////
585//// }
586//// set
587//// {
588//// this[IndexOf(index)]=value;
589//// }
590//// }
591//// public DBParameter this[int index]
592//// {
593//// get
594//// {
595//// return (DBParameter)this[index];
596//// }
597//// set
598//// {
599//// this[index]=value;
600//// }
601//// }
602//
603// public bool Contains(string parameterName)
604// {
605// return(-1 != IndexOf(parameterName));
606//
607// }
608//
609// public int IndexOf(string parameterName)
610// {
611// int index = 0;
612// foreach(System.Data.IDataParameter item in this)
613// {
614// if (0 == _cultureAwareCompare(item.ParameterName,parameterName))
615// {
616// return index;
617// }
618// index++;
619// }
620// return -1;
621// }
622//
623// public void RemoveAt(string parameterName)
624// {
625// RemoveAt(IndexOf(parameterName));
626// }
627//
628// public override int Add(object value)
629// {
630// return Add((DBParameter)value);
631// }
632//
633// public int Add(DBParameter valueIn)
634// {
635// if (valueIn.ParameterName != null)
636// {
637// return base.Add(valueIn);
638// }
639// else
640// throw new ArgumentException("parameter must be named");
641// }
642//
643// public int Add(string parameterName, System.Data.DbType type)
644// {
645// return Add(new DBParameter(parameterName, type));
646// }
647//
648// public int Add(string parameterName, object value)
649// {
650// return Add(new DBParameter(parameterName, value));
651// }
652//
653// public int Add(string parameterName, DbType dbType, string sourceColumn)
654// {
655// return Add(new DBParameter(parameterName, dbType, sourceColumn));
656// }
657//
658// private int _cultureAwareCompare(string strA, string strB)
659// {
660// return System.Globalization.CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, System.Globalization.CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth | CompareOptions.IgnoreCase);
661// }
662//
663//
664//
665//
666// }
667
668// public abstract class datareader:system.data.idatareader
669// {
670// public abstract int depth {get;}
671// public abstract bool isclosed{get;}
672// public abstract int recordsaffected{get;}
673// public abstract void close();
674// public abstract bool nextresult();
675// public abstract bool read();
676// public abstract datatable getschematable();
677// public abstract int fieldcount{get;}
678// public abstract string getname(int i);
679// public abstract string getdatatypename(int i);
680// public abstract type getfieldtype(int i);
681// public abstract object getvalue(int i);
682// public abstract int getvalues(object[] values);
683// public abstract int getordinal(string name);
684// public abstract object this [ int i ]{get;}
685// public abstract object this [ string name ]{get;}
686// public abstract bool getboolean(int i);
687// public abstract byte getbyte(int i);
688// public abstract long getbytes(int i, long fieldoffset, byte[] buffer, int bufferoffset, int length);
689// public abstract char getchar(int i);
690// public abstract long getchars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length);
691// public abstract guid getguid(int i);
692// public abstract int16 getint16(int i);
693// public abstract int32 getint32(int i);
694// public abstract int64 getint64(int i);
695// public abstract float getfloat(int i);
696// public abstract double getdouble(int i);
697// public abstract string getstring(int i);
698// public abstract decimal getdecimal(int i);
699// public abstract datetime getdatetime(int i);
700// public abstract idatareader getdata(int i);
701// public abstract bool isdbnull(int i);
702// public abstract void dispose();
703// //private abstract int _cultureawarecompare(string stra, string strb);
704//
705// }
706
707
708}</startcount;i++)>