Tool.cs
上传用户:xuming1973
上传日期:2014-02-27
资源大小:17511k
文件大小:2k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

C#

  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Collections;
  5. namespace UDS.Components
  6. {
  7. /// <summary>
  8. /// 常用工具函数类
  9. /// </summary>
  10. public class Tools
  11. {
  12. #region 将DataReader 转为 DataTable
  13. /// <summary>
  14. /// 将DataReader 转为 DataTable
  15. /// </summary>
  16. /// <param name="DataReader">DataReader</param>
  17. public static DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
  18. {
  19. DataTable datatable = new DataTable();
  20. DataTable schemaTable = dataReader.GetSchemaTable();
  21. //动态添加列
  22. try
  23. {
  24. foreach(DataRow myRow in schemaTable.Rows)
  25. {
  26. DataColumn myDataColumn = new DataColumn();
  27. myDataColumn.DataType = myRow.GetType();
  28. myDataColumn.ColumnName = myRow[0].ToString();
  29. datatable.Columns.Add(myDataColumn);
  30. }
  31. //添加数据
  32. while(dataReader.Read())
  33. {
  34. DataRow myDataRow = datatable.NewRow();
  35. for(int i=0;i<schemaTable.Rows.Count;i++)
  36. {
  37. myDataRow[i] = dataReader[i].ToString();
  38. }
  39. datatable.Rows.Add(myDataRow);
  40. myDataRow = null;
  41. }
  42. schemaTable = null;
  43. dataReader.Close();
  44. return datatable;
  45. }
  46. catch(Exception ex)
  47. {
  48. Error.Log(ex.ToString());
  49. throw new Exception("转换出错出错!",ex);
  50. }
  51. }
  52. #endregion
  53. #region 将英文的星期几转为中文
  54. public static string ConvertDayOfWeekToZh(System.DayOfWeek dw)
  55. {
  56. string DayOfWeekZh="";
  57. switch (dw.ToString ("D"))
  58. {
  59. case "0":
  60. DayOfWeekZh="日";
  61. break;
  62. case "1":
  63. DayOfWeekZh="一";
  64. break;
  65. case "2":
  66. DayOfWeekZh="二";
  67. break;
  68. case "3":
  69. DayOfWeekZh="三";
  70. break;
  71. case "4":
  72. DayOfWeekZh="四";
  73. break;
  74. case "5":
  75. DayOfWeekZh="五";
  76. break;
  77. case "6":
  78. DayOfWeekZh="六";
  79. break;
  80. }
  81. return DayOfWeekZh;
  82. }
  83. #endregion
  84. }
  85. }