自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#數(shù)據(jù)訪問(wèn)層的相關(guān)知識(shí)

開發(fā) 后端
這里將為大家介紹C#數(shù)據(jù)訪問(wèn)層的相關(guān)知識(shí),并通過(guò)實(shí)際代碼的形式來(lái)便于大家理解。希望本文對(duì)大家有所幫助。

C#數(shù)據(jù)訪問(wèn)層1.查詢數(shù)據(jù)庫(kù)中的數(shù)據(jù),返回一個(gè)datatable

C#數(shù)據(jù)訪問(wèn)層2.執(zhí)行一條SQL語(yǔ)句已重載

  1. using System;  
  2. using System.Data;  
  3. using NUnit.Framework;  
  4. using CodeFilemanger.Project;  
  5. using System.Data.SqlClient;  
  6. using System.Configuration;  
  7. namespace OperatorDB  
  8. {  
  9.  
  10. ///   
  11. /// Class1 的摘要說(shuō)明。  
  12. ///   
  13. [NUnit.Framework.TestFixture]  
  14. public class OperatorDB   
  15. {   
  16. private static string strCon = ConfigurationSettings.AppSettings["ConnectionString"] ;   
  17. private int ModuleId = 1;  
  18. public static string ConnectionString   
  19. {   
  20. get   
  21. {   
  22. return strCon;   
  23. }   
  24. set   
  25. {   
  26. strCon = value;   
  27. }   
  28.  
  29.  
  30. #region "初始化"   
  31. [NUnit.Framework.TestFixtureSetUp]  
  32. public void Register_Module()  
  33. {   
  34. string ModuleName = "OperatorDB";  
  35. string ModuleAuthor = "MYM";  
  36. string ModuleDescribe = "數(shù)據(jù)訪問(wèn)模塊";  
  37. string CreateDatetime = "2003-5-30";  
  38. ModuleId = Project.InsertModule( ModuleName, ModuleAuthor, ModuleDescribe, CreateDatetime) ;   
  39. }  
  40. [Test]  
  41. public void Register_Method_SelectData()  
  42. {  
  43. string MethodName = "SelectData";  
  44. string MethodAuthor = "MYM";  
  45. string MethodCreateDateTime = "2005-3-30";  
  46. string MethodParaMeters ="ParaMeters(string SqlCommandText, System.Data.DataTable Dt, bool RowsClearr)";  
  47. string MethodReturn = "bool";  
  48. string MethodCall = "" ;  
  49. string MethodDescribe = "查詢數(shù)據(jù)庫(kù)中的數(shù)據(jù),返回一個(gè)datatable";  
  50. Project.InsertMethod(MethodName,MethodAuthor,MethodCreateDateTime,MethodParaMeters,MethodReturn,MethodCall,MethodDescribe,ModuleId);   
  51. }  
  52. [Test]  
  53. public void Register_Method_ExecuteSql()  
  54. {  
  55. string MethodName = "ExecuteSql";  
  56. string MethodAuthor = "MYM";  
  57. string MethodCreateDateTime = "2005-3-30";  
  58. string MethodParaMeters ="ParaMeters(string SqlCommandText)";  
  59. string MethodReturn = "int";  
  60. string MethodCall = "" ;  
  61. string MethodDescribe = "執(zhí)行一條SQL語(yǔ)句";  
  62. Project.InsertMethod(MethodName,MethodAuthor,MethodCreateDateTime,MethodParaMeters,MethodReturn,MethodCall,MethodDescribe,ModuleId);   
  63. }  
  64. [Test]  
  65. public void Register_Method_SerialNumber()  
  66. {  
  67. string MethodName = "SerialNumber";  
  68. string MethodAuthor = "MYM";  
  69. string MethodCreateDateTime = "2005-3-30";  
  70. string MethodParaMeters ="ParaMeters(int index, System.Data.DataTable dt)";  
  71. string MethodReturn = "void";  
  72. string MethodCall = "" ;  
  73. string MethodDescribe = "給表的指定列添加序號(hào)";  
  74. Project.InsertMethod(MethodName,MethodAuthor,MethodCreateDateTime,MethodParaMeters,MethodReturn,MethodCall,MethodDescribe,ModuleId);   
  75. }  
  76.  
  77. #endregion   
  78. public static bool SelectData(string SqlCommandText, System.Data.DataTable Dt, bool RowsClearr)   
  79. {   
  80. strCon = ConfigurationSettings.AppSettings["ConnectionString"];   
  81. bool ret = true;   
  82. if (SqlCommandText != "")   
  83. {   
  84. if (RowsClearr)   
  85. {   
  86. if (Dt.Rows.Count > 0)  
  87. {  
  88. Dt.Rows.Clear();   
  89. }  
  90.  
  91. }   
  92. SqlConnection cn = new SqlConnection(strCon);   
  93. SqlDataAdapter da = new SqlDataAdapter(SqlCommandText, cn);   
  94. try   
  95. {   
  96. cn.Open();   
  97. da.Fill(Dt);   
  98. }   
  99. catch (System.Exception ex)   
  100. {   
  101. ExceptionHand exc = new ExceptionHand(ex);   
  102. exc.DisplayErrorMessager("OperatorDB","SelectData",SqlCommandText);   
  103. ret = false;   
  104. }   
  105. if (cn.State == ConnectionState.Open)   
  106. {   
  107. cn.Close();   
  108. }   
  109. da.Dispose();   
  110. }   
  111. else   
  112. {   
  113. ret = false;   
  114. }   
  115. return ret;   
  116. }   
  117. public static int ExecuteSql(string SqlCommandText)   
  118. {   
  119. int ID = 0;   
  120. strCon = ConfigurationSettings.AppSettings["ConnectionString"];   
  121. if (SqlCommandText != "")   
  122. {   
  123. SqlConnection cn = new SqlConnection(strCon);   
  124. SqlCommand cm = new SqlCommand(SqlCommandText, cn);   
  125. try   
  126. {   
  127. cn.Open();  
  128. ID = Convert.ToInt32(cm.ExecuteScalar());  
  129. }   
  130. catch (System.Exception ex)   
  131. {   
  132. cn.Close();  
  133. ExceptionHand exc = new ExceptionHand(ex);   
  134. exc.DisplayErrorMessager("OperatorDB","ExecuteSql",SqlCommandText);   
  135. ID = -1;   
  136. }   
  137.  
  138. if (cn.State == ConnectionState.Open)   
  139. {   
  140. cn.Close();   
  141. }   
  142. cm.Dispose();   
  143. }   
  144. return ID;   
  145. }   
  146. public static int ExecuteSql(SqlCommand Cm)   
  147. {   
  148. int ID = 0;   
  149. strCon = ConfigurationSettings.AppSettings["ConnectionString"];   
  150. SqlConnection cn = new SqlConnection(strCon);   
  151. try   
  152. {   
  153. cn.Open();   
  154. Cm.Connection = cn;  
  155. ID = Convert.ToInt32(Cm.ExecuteScalar());   
  156. }   
  157. catch (System.Exception ex)   
  158. {   
  159. cn.Close();  
  160. ExceptionHand exc = new ExceptionHand(ex);   
  161. exc.DisplayErrorMessager("OperatorDB","ExecuteSql",Cm.CommandText);   
  162. ID = -1;   
  163. }   
  164.  
  165. if (cn.State == ConnectionState.Open)   
  166. {   
  167. cn.Close();   
  168. }   
  169. Cm.Dispose();   
  170.  
  171. return ID;   
  172. }   
  173. public static void SerialNumber(int index, System.Data.DataTable dt)   
  174. {   
  175. for (int i = 0; i <= dt.Rows.Count - 1; i++)   
  176. {   
  177. dt.Rows[i][index] = i + 1;   
  178. }   
  179. }   
  180. public static void SetSqlCommandValues(SqlCommand Com,DataTable Dt,int Index,int StartIndex)  
  181. {  
  182. int i;  
  183. for (i=StartIndex;i{  
  184. Com.Parameters.Add("@" + Dt.Columns[i].ColumnName,Dt.Rows[Index][i]);  
  185. }  
  186. }   
  187. }  

C#數(shù)據(jù)訪問(wèn)層的相關(guān)知識(shí)就介紹到這里。

【編輯推薦】

  1. C#結(jié)構(gòu)體的特點(diǎn)淺析
  2. 介紹C#窗體拖動(dòng)事件
  3. C#讀取Excel遇到無(wú)法讀取的解決方法
  4. 概述C#.NET操作XML
  5. C#基礎(chǔ)概念學(xué)習(xí)筆記
責(zé)任編輯:彭凡 來(lái)源: itpub.net
相關(guān)推薦

2009-09-04 18:00:54

C#數(shù)據(jù)訪問(wèn)層

2009-08-21 08:41:44

C#反射

2009-08-28 10:22:47

C# DLLImpor

2009-09-01 16:14:08

C# Socket類

2009-08-10 14:03:08

C# COM接口

2009-08-07 13:30:20

C# Excel導(dǎo)入

2009-09-01 15:25:01

C#位域

2024-11-08 09:44:44

數(shù)據(jù)庫(kù)C#數(shù)據(jù)源

2009-06-12 09:22:44

VB.NET類型C#

2011-03-17 15:59:37

c#數(shù)據(jù)庫(kù)

2009-08-12 14:27:36

訪問(wèn)MySQL數(shù)據(jù)庫(kù)C# ODBC

2009-09-15 15:40:25

C# 綁定

2009-08-26 15:53:42

C#數(shù)據(jù)訪問(wèn)XML

2009-08-06 15:12:22

C#異常機(jī)制

2009-08-05 18:28:05

C#異常處理

2024-05-20 00:00:00

C#屬性Property

2009-08-28 15:16:32

C#實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)訪問(wèn)

2009-08-07 18:07:58

C#數(shù)據(jù)庫(kù)開發(fā)

2009-08-14 13:52:18

C#判斷數(shù)據(jù)類型

2009-07-30 18:20:21

C#繼承
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)