using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.IO;
using System.Diagnostics;
namespace Components
{
/// <summary>
/// 系统常用工具函数类
/// </summary>
public class SystemTools
{
/// <summary>
/// 将DataReader 转为 DataTable
/// </summary>
/// <param name="DataReader">DataReader</param>
public static DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
{
DataTable datatable = new DataTable();
DataTable schemaTable = dataReader.GetSchemaTable();
//动态添加列
try
{
foreach(DataRow myRow in schemaTable.Rows)
{
DataColumn myDataColumn = new DataColumn();
myDataColumn.DataType = myRow.GetType();
myDataColumn.ColumnName = myRow[0].ToString();
datatable.Columns.Add(myDataColumn);
}
//添加数据
while(dataReader.Read())
{
DataRow myDataRow = datatable.NewRow();
for(int i=0;i<schemaTable.Rows.Count;i++)
{
myDataRow[i] = dataReader[i].ToString();
}
datatable.Rows.Add(myDataRow);
myDataRow = null;
}
schemaTable = null;
dataReader.Close();
return datatable;
}
catch(Exception ex)
{
///抛出类型转换错误
throw new Exception("转换出错出错!",ex);
}
}
/// <summary>
/// 将英文的星期几转为中文
/// </summary>
public static string ConvertDayOfWeekToZh(System.DayOfWeek dw)
{
string DayOfWeekZh="";
switch (dw.ToString ("D"))
{
case "0":
DayOfWeekZh="日";
break;
case "1":
DayOfWeekZh="一";
break;
case "2":
DayOfWeekZh="二";
break;
case "3":
DayOfWeekZh="三";
break;
case "4":
DayOfWeekZh="四";
break;
case "5":
DayOfWeekZh="五";
break;
case "6":
DayOfWeekZh="六";
break;
}
return DayOfWeekZh;
}
}
/// <summary>
/// 错误处理函数,用于记录错误日志
/// </summary>
public class SystemError
{
//记录错误日志位置
private const string FILE_NAME = "c://OfficeAutolog.txt";
/// <summary>
/// 记录日志至文本文件
/// </summary>
/// <param name="message">记录的内容</param>
public static void SystemLog(string message)
{
// if(File.Exists(FILE_NAME))
// {
// ///如果日志文件已经存在,则直接写入日志文件
// StreamWriter sr = File.AppendText(FILE_NAME);
// sr.WriteLine ("/n");
// sr.WriteLine (DateTime.Now.ToString()+message);
// sr.Close();
// }
// else
// {
// ///创建日志文件
// StreamWriter sr = File.CreateText(FILE_NAME);
// sr.Close();
// }
}
}
//自定义Exception
public class MyException:Exception
{
//包含系统Excepton
public MyException(string source,string message,Exception inner):base(message,inner)
{
base.Source=source;
}
//不包含系统Exception
public MyException(string source,string message):base(message)
{
base.Source=source;
}
}
/// <summary>
/// 处理网页中的HTML代码,并消除危险字符
/// </summary>
public class SystemHTML
{
private static string HTMLEncode(string fString)
{
if(fString!=string.Empty)
{
///替换尖括号
fString.Replace("<","<");
fString.Replace(">","&rt;");
///替换引号
fString.Replace(((char)34).ToString(), """);
fString.Replace(((char)39).ToString(), "'");
///替换空格
fString.Replace(((char)13).ToString(), "");
///替换换行符
fString.Replace(((char)10).ToString(), "<BR> ");
}
return(fString);
}
}
}