C#数据访问层的相关知识

开发 后端
这里将为大家介绍C#数据访问层的相关知识,并通过实际代码的形式来便于大家理解。希望本文对大家有所帮助。

C#数据访问层1.查询数据库中的数据,返回一个datatable

C#数据访问层2.执行一条SQL语句已重载

  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 的摘要说明。  
  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 = "数据访问模块";  
  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 = "查询数据库中的数据,返回一个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 = "执行一条SQL语句";  
  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 = "给表的指定列添加序号";  
  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#数据访问层的相关知识就介绍到这里。

【编辑推荐】

  1. C#结构体的特点浅析
  2. 介绍C#窗体拖动事件
  3. C#读取Excel遇到无法读取的解决方法
  4. 概述C#.NET操作XML
  5. C#基础概念学习笔记
责任编辑:彭凡 来源: itpub.net
相关推荐

2009-09-04 18:00:54

C#数据访问层

2009-08-28 10:22:47

C# DLLImpor

2009-08-21 08:41:44

C#反射

2009-09-01 16:14:08

C# Socket类

2009-08-10 14:03:08

C# COM接口

2009-08-07 13:30:20

C# Excel导入

2024-11-08 09:44:44

数据库C#数据源

2009-09-01 15:25:01

C#位域

2009-06-12 09:22:44

VB.NET类型C#

2011-03-17 15:59:37

c#数据库

2009-08-12 14:27:36

访问MySQL数据库C# ODBC

2009-09-15 15:40:25

C# 绑定

2009-08-26 15:53:42

C#数据访问XML

2009-08-06 15:12:22

C#异常机制

2009-08-05 18:28:05

C#异常处理

2009-08-28 15:16:32

C#实现对数据库访问

2024-05-20 00:00:00

C#属性Property

2009-08-07 18:07:58

C#数据库开发

2009-08-14 13:52:18

C#判断数据类型

2009-07-30 18:20:21

C#继承
点赞
收藏

51CTO技术栈公众号