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

C#数据访问层的相关知识就介绍到这里。


网页题目:C#数据访问层的相关知识
分享链接:http://wkslyf.com/article/dppgige.html