[Oracle]ASP.NET+Oracle连接类conn.cs

GoDotNet社区下的: http://files.cnblogs.com/volnet/DataLayer.zip 大家看这个吧
我这个当个反面教材就好了。

嗨,辛苦地写了半天~~~草草了事了~~先发上来,大家帮忙看看有啥不好噢~!多提意见会对我很有帮助滴~~~!
目的:好好地写一个类处理大部分常用的oracle数据库连接的问题。
当然,在开始还是把基本设置做齐了,省得一会连不通被人骂 emteeth.gif

首先,配置web.config文件
添加
None.gif    < appSettings >
None.gif    
< add  key ="SysDSN"  value ="Data Source=ServerName;User ID=userid;Password=password;Unicode=True" />
None.gif  
</ appSettings >
再者,就可以添加一个myclass文件夹,之后在其中建个类文件,默认命名空间是myClass
类文件名为conn.cs
由于VS.NET2005不默认支持oracle数据库,所以要添加其引用:在解决方案资源管理器里添加引用:System.Data.OracleClient(仔细找找噢~在.NET选项卡中)
内容(不断更新):
  1 None.gif using  System;
  2 None.gif using  System.Data;
  3 None.gif using  System.Configuration;
  4 None.gif using  System.Web;
  5 None.gif using  System.Web.Security;
  6 None.gif using  System.Web.UI;
  7 None.gif using  System.Web.UI.WebControls;
  8 None.gif using  System.Web.UI.WebControls.WebParts;
  9 None.gif using  System.Web.UI.HtmlControls;
 10 None.gif using  System.Data.OracleClient;
 11 None.gif
 12 None.gif namespace  User.Data
 13 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 14InBlock.gif    public class conn
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 16InBlock.gif
 17ContractedSubBlock.gifExpandedSubBlockStart.gif        构造函数#region 构造函数
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 19InBlock.gif        /// 构造函数(默认)
 20ExpandedSubBlockEnd.gif        /// </summary>

 21ExpandedSubBlockStart.gifContractedSubBlock.gif        public conn() dot.gif{ }
 22InBlock.gif
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 24InBlock.gif        /// 构造函数
 25InBlock.gif        /// </summary>
 26InBlock.gif        /// <param name="connString">连接字符串(类型:string)</param>
 27ExpandedSubBlockEnd.gif        /// 例子:"Data Source=ServerName;User ID=userid;Password=password;Unicode=True"

 28InBlock.gif        public conn(string connString)
 29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 30InBlock.gif            this.ConnectionString = connString;
 31ExpandedSubBlockEnd.gif        }

 32InBlock.gif
 33ExpandedSubBlockEnd.gif        #endregion

 34InBlock.gif
 35ContractedSubBlock.gifExpandedSubBlockStart.gif        私有变量#region 私有变量
 36InBlock.gif
 37ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 38InBlock.gif        /// 表示针对数据库执行的SQL语句或存储过程
 39ExpandedSubBlockEnd.gif        /// </summary>

 40InBlock.gif        private System.Data.OracleClient.OracleCommand cmd;
 41InBlock.gif
 42ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 43InBlock.gif        /// 表示一个到数据库的打开的连接
 44ExpandedSubBlockEnd.gif        /// </summary>

 45InBlock.gif        private System.Data.OracleClient.OracleConnection con;
 46InBlock.gif
 47ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 48InBlock.gif        /// 表示要在数据库中生成的事务
 49ExpandedSubBlockEnd.gif        /// </summary>

 50InBlock.gif        private System.Data.OracleClient.OracleTransaction tran;
 51InBlock.gif
 52ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 53InBlock.gif        /// 表示用于填充System.Data.DataSet和更新数据库的一组数据命令和到数据库的连接。
 54ExpandedSubBlockEnd.gif        /// </summary>

 55InBlock.gif        private System.Data.OracleClient.OracleDataAdapter adapter;
 56InBlock.gif        
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 58InBlock.gif        /// 提供从数据源读取数据行的只进流的方法
 59ExpandedSubBlockEnd.gif        /// </summary>

 60InBlock.gif        private System.Data.OracleClient.OracleDataReader reader;
 61InBlock.gif
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 63InBlock.gif        /// 表示内存中数据的一个表
 64ExpandedSubBlockEnd.gif        /// </summary>

 65InBlock.gif        private System.Data.DataTable dt;
 66InBlock.gif
 67ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 68InBlock.gif        /// 表示数据在内存中的缓存
 69ExpandedSubBlockEnd.gif        /// </summary>

 70InBlock.gif        private System.Data.DataSet ds;
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 72InBlock.gif        /// 连接字符串
 73ExpandedSubBlockEnd.gif        /// </summary>

 74InBlock.gif        string ConnectionString = "";
 75InBlock.gif
 76ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 77InBlock.gif        /// 返回与打开的数据库连接
 78ExpandedSubBlockEnd.gif        /// </summary>

 79InBlock.gif        private System.Data.OracleClient.OracleConnection openedCon
 80ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 81InBlock.gif            get 
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 83InBlock.gif                this.openCon(); //固定地打开数据库与之连接
 84InBlock.gif                return this.con; 
 85ExpandedSubBlockEnd.gif            }

 86InBlock.gif            //connectiongString是由固定的,存放在Web.config文件的AppSetting节点下,因此无需set访问器
 87ExpandedSubBlockEnd.gif        }

 88ExpandedSubBlockEnd.gif        #endregion

 89InBlock.gif
 90ContractedSubBlock.gifExpandedSubBlockStart.gif        私有方法#region 私有方法
 91ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 92InBlock.gif        /// 打开与数据库的连接
 93ExpandedSubBlockEnd.gif        /// </summary>

 94InBlock.gif        private void openCon()
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 96InBlock.gif            try
 97ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 98InBlock.gif                if (this.con == null)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
100InBlock.gif                    //使用using可以使该连接可以调用Dispose方法来释放资源
101InBlock.gif                    //using (this.con = new OracleConnection())
102InBlock.gif                    //{
103InBlock.gif                        this.con = new OracleConnection();
104InBlock.gif                        //设置数据库连接属性为web.config中的设置的值(默认)
105InBlock.gif                        //或者设置为构造函数中指定的connString的值
106InBlock.gif                        this.con.ConnectionString 
107InBlock.gif                            = (this.ConnectionString == ""? System.Configuration.ConfigurationManager.AppSettings["SysDSN"] : this.ConnectionString;
108InBlock.gif                        this.con.Open();
109InBlock.gif                    //}
110InBlock.gif                    //System.Web.HttpContext.Current.Response.Write("数据库连接成功!"); //Test
111ExpandedSubBlockEnd.gif                }

112InBlock.gif                else if (con.State == ConnectionState.Closed)
113ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
114InBlock.gif                    this.con.Open();
115ExpandedSubBlockEnd.gif                }

116ExpandedSubBlockEnd.gif            }

117InBlock.gif            catch
118ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
119InBlock.gif                System.Web.HttpContext.Current.Response.Write("数据库连接失败,请与管理员联系!");
120InBlock.gif                System.Web.HttpContext.Current.Response.End();
121ExpandedSubBlockEnd.gif            }

122ExpandedSubBlockEnd.gif        }

123InBlock.gif
124ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
125InBlock.gif        /// 获取或设置将在其中执行System.Data.OracleClient.OracleCommand的
126InBlock.gif        /// System.Data.OracleClient.OracleTransaction。
127InBlock.gif        /// 因为OracleConnection 不支持并行事务。所以在添加事务前必须要检查是否为空!
128ExpandedSubBlockEnd.gif        /// </summary>

129InBlock.gif        private void checkTransaction()
130ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
131InBlock.gif            if (this.tran != null)
132ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
133InBlock.gif                this.cmd.Transaction = this.tran;
134ExpandedSubBlockEnd.gif            }

135ExpandedSubBlockEnd.gif        }

136InBlock.gif
137ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
138InBlock.gif        /// 设置基本Command对象
139InBlock.gif        /// </summary>
140ExpandedSubBlockEnd.gif        /// <param name="sql"></param>

141InBlock.gif        private void CreateCmd(string sql)
142ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
143InBlock.gif            //方法1
144InBlock.gif            this.cmd = new OracleCommand();
145InBlock.gif            this.checkTransaction();
146InBlock.gif            this.cmd.Connection = this.openedCon;
147InBlock.gif            this.cmd.CommandText = sql;
148InBlock.gif            
149InBlock.gif            //方法2
150InBlock.gif            //this.checkTransaction();
151InBlock.gif            //this.cmd=new OracleCommand(sql);
152InBlock.gif            //this.cmd.Connection=this.openedCon;
153InBlock.gif
154InBlock.gif            //方法3
155InBlock.gif            //this.checkTransaction();
156InBlock.gif            //this.cmd = new OracleCommand(sql, this.openedCon);
157InBlock.gif
158InBlock.gif            //方法4
159InBlock.gif            //this.cmd = new OracleCommand(sql, this.openedCon, this.tran);
160InBlock.gif
161InBlock.gif            //方法5(Oracle中只支持.NET2.0以上版本)
162InBlock.gif            //this.cmd = this.openedCon.CreateCommand();
163InBlock.gif            //this.checkTransaction();
164InBlock.gif            //this.cmd.CommandText = sql;
165ExpandedSubBlockEnd.gif        }

166InBlock.gif
167ExpandedSubBlockEnd.gif        #endregion

168InBlock.gif
169ContractedSubBlock.gifExpandedSubBlockStart.gif        公共方法#region 公共方法
170InBlock.gif
171ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
172InBlock.gif        /// 开始事务
173ExpandedSubBlockEnd.gif        /// </summary>

174InBlock.gif        public void BeginTransaction()
175ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
176InBlock.gif            this.tran = this.openedCon.BeginTransaction();
177ExpandedSubBlockEnd.gif        }

178ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
179InBlock.gif        /// 提交事务
180ExpandedSubBlockEnd.gif        /// </summary>

181InBlock.gif        public void Commit()
182ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
183InBlock.gif            this.tran.Commit();
184ExpandedSubBlockEnd.gif        }

185ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
186InBlock.gif        /// 回滚事务
187ExpandedSubBlockEnd.gif        /// </summary>

188InBlock.gif        public void RollBack()
189ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
190InBlock.gif            this.tran.Rollback();   
191ExpandedSubBlockEnd.gif        }

192InBlock.gif
193ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
194InBlock.gif        /// 关闭与数据库的连接
195ExpandedSubBlockEnd.gif        /// </summary>

196InBlock.gif        public void CloseCon()
197ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
198InBlock.gif            if (this.openedCon != null && this.openedCon.State == ConnectionState.Open)
199ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
200InBlock.gif                this.openedCon.Close();
201ExpandedSubBlockEnd.gif            }

202ExpandedSubBlockEnd.gif        }

203InBlock.gif
204ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
205InBlock.gif        /// 执行SQL语句
206InBlock.gif        /// </summary>
207InBlock.gif        /// <param name="sql">SQL语句</param>
208ExpandedSubBlockEnd.gif        /// <returns>受影响的行数</returns>

209InBlock.gif        public int DoSelectSql(string sql)
210ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
211InBlock.gif            this.CreateCmd(sql);
212InBlock.gif            return this.cmd.ExecuteNonQuery();
213ExpandedSubBlockEnd.gif        }

214InBlock.gif
215ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
216InBlock.gif        /// 获得OracleDataReader对象
217InBlock.gif        /// </summary>
218InBlock.gif        /// <param name="sql">SQL语句</param>
219ExpandedSubBlockEnd.gif        /// <returns>OracleDataReader对象</returns>

220InBlock.gif        public System.Data.OracleClient.OracleDataReader GetReader(string sql)
221ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
222InBlock.gif            this.CreateCmd(sql);
223InBlock.gif            this.reader=this.cmd.ExecuteReader();
224InBlock.gif            return this.reader;
225ExpandedSubBlockEnd.gif        }

226InBlock.gif
227ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
228InBlock.gif        /// 获得DataTable
229InBlock.gif        /// </summary>
230InBlock.gif        /// <param name="sql">SQL语句</param>
231ExpandedSubBlockEnd.gif        /// <returns>DataTable对象</returns>

232InBlock.gif        public System.Data.DataTable GetDataTable(string sql)
233ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
234InBlock.gif            this.adapter = new OracleDataAdapter();
235InBlock.gif            this.dt = new DataTable();
236InBlock.gif            this.CreateCmd(sql);
237InBlock.gif            this.adapter.SelectCommand = this.cmd;
238InBlock.gif            this.adapter.Fill(this.dt);
239InBlock.gif            return this.dt;
240ExpandedSubBlockEnd.gif        }

241InBlock.gif
242ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
243InBlock.gif        /// 获得DataSet(通过sql语句)
244InBlock.gif        /// </summary>
245InBlock.gif        /// <param name="sql">SQL语句</param>
246ExpandedSubBlockEnd.gif        /// <returns>DataSet对象</returns>

247InBlock.gif        public System.Data.DataSet GetDataSet(string sql)
248ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
249InBlock.gif            this.adapter = new OracleDataAdapter();
250InBlock.gif            this.ds = new DataSet();
251InBlock.gif            this.CreateCmd(sql);
252InBlock.gif            this.adapter.SelectCommand = this.cmd;
253InBlock.gif            this.adapter.Fill(this.ds);
254InBlock.gif            return this.ds;
255ExpandedSubBlockEnd.gif        }

256InBlock.gif
257ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
258InBlock.gif        /// 获得DataSet(通过sql语句、表名)
259InBlock.gif        /// </summary>
260InBlock.gif        /// <param name="sql">SQL语句</param>
261InBlock.gif        /// <param name="srcTableName">用于表映射的源表的名称</param>
262ExpandedSubBlockEnd.gif        /// <returns>DataSet对象</returns>

263InBlock.gif        public System.Data.DataSet GetDataSet(string sql,string srcTableName)
264ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
265InBlock.gif            if (this.ds == null)
266ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
267InBlock.gif                this.ds = new DataSet();
268ExpandedSubBlockEnd.gif            }

269InBlock.gif            this.adapter = new OracleDataAdapter();
270InBlock.gif            this.CreateCmd(sql);
271InBlock.gif            this.adapter.SelectCommand = this.cmd;
272InBlock.gif            this.adapter.Fill(ds,srcTableName);
273InBlock.gif            return this.ds;
274ExpandedSubBlockEnd.gif        }

275InBlock.gif
276ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
277InBlock.gif        /// 获得DataSet(通过sql语句、表名、已存在的DataSet)
278InBlock.gif        /// </summary>
279InBlock.gif        /// <param name="sql">SQL语句</param>
280InBlock.gif        /// <param name="srcTableName">用于表映射的源表的名称</param>
281InBlock.gif        /// <param name="DataSet">已存在的DataSet对象</param>
282ExpandedSubBlockEnd.gif        /// <returns>DataSet对象</returns>

283InBlock.gif        public System.Data.DataSet GetDataSet(string sql, string srcTableName, DataSet DataSet)
284ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
285InBlock.gif            this.ds = DataSet;
286InBlock.gif            return GetDataSet(sql,srcTableName);
287ExpandedSubBlockEnd.gif        }

288InBlock.gif
289InBlock.gif
290ExpandedSubBlockEnd.gif        #endregion

291ExpandedSubBlockEnd.gif    }

292ExpandedBlockEnd.gif}

293 None.gif

最后就是调用它们了,不过这个不是本文的重点,就随便显示显示就OK了。
随便拉两个GridView控件,在它们的Page_Load事件中添加代码:(注意红色部分要和实际相符)
None.gif          protected   void  Page_Load( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            User.Data.conn myConn 
= new conn();
InBlock.gif            
string sql = "select t.cnt_id,t.cnt_title from dat_content t where cnt_id=275";
InBlock.gif            System.Data.DataTable dt 
= myConn.GetDataTable(sql);
InBlock.gif            GridView1.DataSource 
= dt;
InBlock.gif            GridView1.DataBind();
InBlock.gif
InBlock.gif            sql 
= "select t.cnt_id,t.cnt_title from dat_content t where cnt_id=282";
InBlock.gif            System.Data.DataSet ds 
= myConn.GetDataSet(sql);
InBlock.gif            GridView2.DataSource 
= ds;
InBlock.gif            GridView2.DataBind();
InBlock.gif
InBlock.gif            myConn.CloseCon();
InBlock.gif
ExpandedBlockEnd.gif        }
在页面cs文件中添加引用(如果你在默认页搞测试的话,那么就是Default.aspx.cs):
None.gif using  User.Data;

基本上就OK了,Ctrl+F5就OK了。记得把两条sql语句改改~~~~`还有web.config中的连接名和连接密码要和实际的相符,否则不是连不上就是查不到东东噢~~~

conn.cs下载地址: http://files.cnblogs.com/volnet/conn[Oracle].rar

转载于:https://www.cnblogs.com/volnet/archive/2007/01/22/627190.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值