Common System Tools and functions: date conversion, able, datareader

Source: Internet
Author: User

Using system;
Using system. Data;
Using system. Data. sqlclient;
Using system. collections;
Using system. IO;
Using system. diagnostics;

Namespace Components
{
/// <Summary>
/// Common system tools and functions
/// </Summary>
Public class systemtools
{
/// <Summary>
/// Convert datareader to datatable
/// </Summary>
/// <Param name = "datareader"> datareader </param>
Public static datatable convertdatareadertodatatable (sqldatareader datareader)
{
Datatable = new datatable ();
Datatable schematable = datareader. getschematable ();
// Add columns dynamically
Try
{

Foreach (datarow myrow in schematable. Rows)
{
Datacolumn mydatacolumn = new datacolumn ();
Mydatacolumn. datatype = myrow. GetType ();
Mydatacolumn. columnname = myrow [0]. tostring ();
Datatable. Columns. Add (mydatacolumn );
}
// Add data
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 a type conversion error
Throw new exception ("Conversion error! ", Ex );
}

}

/// <Summary>
/// Convert the English day of the week into Chinese
/// </Summary>
Public static string convertdayofweektozh (system. dayofweek DW)
{
String dayofweekzh = "";
Switch (DW. tostring ("D "))
{
Case "0 ":
Dayofweekzh = "day ";
Break;
Case "1 ":
Dayofweekzh = "1 ";
Break;
Case "2 ":
Dayofweekzh = "2 ";
Break;
Case "3 ":
Dayofweekzh = "3 ";
Break;
Case "4 ":
Dayofweekzh = "4 ";
Break;
Case "5 ":
Dayofweekzh = "5 ";
Break;
Case "6 ":
Dayofweekzh = "6 ";
Break;
}

Return dayofweekzh;
}
}

/// <Summary>
/// Error handling function, used to record error logs
/// </Summary>
Public class systemerror
{
// Record the location of the Error Log
Private const string file_name = "C: // officeautolog.txt ";

/// <Summary>
/// Record logs to text files
/// </Summary>
/// <Param name = "message"> recorded content </param>
Public static void systemlog (string message)
{
// If (file. exists (file_name ))
//{
//// If the log file already exists, directly write it to the log file
// Streamwriter sr = file. appendtext (file_name );
// Sr. writeline ("/N ");
// Sr. writeline (datetime. Now. tostring () + message );
// Sr. Close ();
//}
// Else
//{
//// Create a log file
// Streamwriter sr = file. createtext (file_name );
// Sr. Close ();
//}
}
}

// Custom exception
Public class myexception: exception
{
// Contains the excepton System
Public myexception (string source, string message, exception inner): Base (message, inner)
{
Base. Source = source;
}

// Does not contain system exception
Public myexception (string source, string message): Base (Message)
{
Base. Source = source;
}
}

/// <Summary>
/// Process HTML code on the webpage and eliminate dangerous characters
/// </Summary>
Public class systemhtml
{
Private Static string htmlencode (string fstring)
{
If (fstring! = String. Empty)
{
/// Replace the angle brackets
Fstring. Replace ("<", "& lt ;");
Fstring. Replace (">", "& RT ;");
/// Replace quotation marks
Fstring. Replace (char) 34). tostring (), "& quot ;");
Fstring. Replace (char) 39). tostring (), "& #39 ;");
/// Replace Spaces
Fstring. Replace (char) 13). tostring (),"");
/// Replace the linefeed
Fstring. Replace (char) 10). tostring (), "<br> ");
}
Return (fstring );
}
}
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.