三層架構(gòu)之?dāng)?shù)據(jù)庫訪問層(doc6頁)_第1頁
三層架構(gòu)之?dāng)?shù)據(jù)庫訪問層(doc6頁)_第2頁
三層架構(gòu)之?dāng)?shù)據(jù)庫訪問層(doc6頁)_第3頁
三層架構(gòu)之?dāng)?shù)據(jù)庫訪問層(doc6頁)_第4頁
三層架構(gòu)之?dāng)?shù)據(jù)庫訪問層(doc6頁)_第5頁
已閱讀5頁,還剩1頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、三層架構(gòu)之?dāng)?shù)據(jù)庫訪問層完全篇(C#)2008-07-15 15:11using System;using System.Data;using System.Data.SqlClient;using System.Configuration;namespace DbBasepublic abstract class Base#region Fields of base calssprotected static string strConn = ConfigurationSettings.AppSettingsstrConnection;protected static string strSQ

2、L;#endregion#region Properties of base class#endregion#region Functions of base classpublic Base()/ TODO: Add constructor logic here/ / executing SQL commands/ / string/ return intprotected static int ExecuteSql(string strSQL)SqlConnection myCn = new SqlConnection(strConn);SqlCommand myCmd = new Sql

3、Command(strSQL,myCn);trymyCn.Open();myCmd.ExecuteNonQuery();return 0;catch(System.Data.SqlClient.SqlException e)throw new Exception(e.Message);finallymyCmd.Dispose();myCn.Close();/ /executing SQL commands/ / 要執(zhí)行的 SQL語句 , 為字符串類型 string/ 返回執(zhí)行情況 , 整形 intprotected static int ExecuteSqlEx(string strSQL)S

4、qlConnection myCn = new SqlConnection(strConn); SqlCommand myCmd = new SqlCommand(strSQL,myCn);trymyCn.Open();SqlDataReader myReader = myCmd.ExecuteReader(); if(myReader.Read()return 0;elsethrow new Exception(Value Unavailable!);catch(System.Data.SqlClient.SqlException e)throw new Exception(e.Messag

5、e);finallymyCmd.Dispose();myCn.Close();/ / get dataset/ / (string)/ (DataSet)protected static DataSet ExecuteSql4Ds(string strSQL)SqlConnection myCn = new SqlConnection(strConn); trymyCn.Open();SqlDataAdapter sda = new SqlDataAdapter(strSQL,myCn); DataSet ds = new DataSet(ds); sda.Fill(ds);return ds

6、;catch(System.Data.SqlClient.SqlException e)throw new Exception(e.Message);finallymyCn.Close();/ / get single value/ / (string)/ (int)protected static int ExecuteSql4Value(string strSQL)SqlConnection myCn = new SqlConnection(strConn); SqlCommand myCmd = new SqlCommand(strSQL,myCn); trymyCn.Open();ob

7、ject r = myCmd.ExecuteScalar();if(Object.Equals(r,null)throw new Exception(value unavailable! );elsereturn (int)r;catch(System.Data.SqlClient.SqlException e)throw new Exception(e.Message);finallymyCmd.Dispose();myCn.Close();/ / get object/ / (string)/ (object)protected static object ExecuteSql4Value

8、Ex(string strSQL)SqlConnection myCn = new SqlConnection(strConn); SqlCommand myCmd = new SqlCommand(strSQL,myCn); trymyCn.Open();object r = myCmd.ExecuteScalar();if(Object.Equals(r,null)throw new Exception(object unavailable!);elsereturn r;catch(System.Data.SqlClient.SqlException e)throw new Excepti

9、on(e.Message);finallymyCmd.Dispose();myCn.Close();/ / execute multipul SQL commands/ / string/ intprotected static int ExecuteSqls(string strSQLs)SqlConnection myCn = new SqlConnection(strConn); SqlCommand myCmd = new SqlCommand();int j=strSQLs.Length;trymyCn.Open();catch(System.Data.SqlClient.SqlEx

10、ception e)throw new Exception(e.Message);SqlTransaction myTrans = myCn.BeginTransaction();trymyCmd.Connection = myCn;myCmd.Transaction = myTrans;foreach(string str in strSQLs)myCmd.CommandText = str;myCmd.ExecuteNonQuery();myTrans.Commit();return 0;catch(System.Data.SqlClient.SqlException e)myTrans.

11、Rollback();throw new Exception(e.Message);finallymyCmd.Dispose();myCn.Close();#endregion人生最大的幸福,莫過于連一分鐘都無法休息零碎的時間實在可以成就大事業(yè)珍惜時間可以使生命變的更有價值時間象奔騰澎湃的急湍,它一去無返,毫不流連一個人越知道時間的價值,就越感到失時的痛苦得到時間,就是得到一切用經(jīng)濟學(xué)的眼光來看,時間就是一種財富時間一點一滴凋謝,猶如蠟燭漫漫燃盡我總是感覺到時間的巨輪在我背后奔馳,日益迫近夜晚給老人帶來平靜,給年輕人帶來希望不浪費時間,每時每刻都做些有用的事,戒掉一切不必要的行為時間乃是萬物中最寶貴的東西,但如果浪費了,那就是最大的浪費我的產(chǎn)業(yè)多么美,多么廣,多么寬,時間是我的財產(chǎn),我的田地是時間時間就是性命,無端的空耗別人的時間,知識是取之不盡,用之不竭的。只有最大限度地挖掘它,才能體會到學(xué)習(xí)的樂趣。新想法常常瞬息即逝,必須集中精力,牢記在心,及時捕獲。每天早晨睜開眼睛,深吸一口氣,給自己一個微笑,然后說:“在這美妙的一天,我又要獲得多少知識??!” 不要為這個世界而驚嘆,要讓這個世界為你而驚嘆!如果說學(xué)習(xí)有捷徑可走,那也一定是勤奮。學(xué)習(xí)猶如

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論