PublicModule(1)

using System;
using System.Data;
using System.Data.SqlClient;

namespace ePOUrgeMail_Service
{
 /// <summary>
 /// Summary description for PublicModule.
 /// </summary>
 public class PublicModule
 {
  public static string LOG_PATH = System.Configuration.ConfigurationSettings.AppSettings["LOG_PATH"];
  public static string CopyTo = System.Configuration.ConfigurationSettings.AppSettings["CopyTo"];
  public static string BlindCopyTo = System.Configuration.ConfigurationSettings.AppSettings["BlindCopyTo"];
  public static string RequestURL = System.Configuration.ConfigurationSettings.AppSettings["RequestURL"];
  public static double Timer_Interval = Convert.ToDouble( System.Configuration.ConfigurationSettings.AppSettings["Timer_Interval"] );
  public static bool bFinished_UrgeMail = true;

  string ePO_CONNECTSTRING = System.Configuration.ConfigurationSettings.AppSettings["ePO_CONNECTSTRING"];

  private SqlConnection myConn;
  private SqlCommand myCommand;

  public PublicModule()
  {
   //
   // TODO: Add constructor logic here
   //
  }

  /// <summary>
  /// 記錄日志
  /// </summary>
  /// <param name="strInput">日志內容</param>
  public static void SaveLog( string strInput )
  {  
   string strFilePath = LOG_PATH;

   System.IO.FileStream fs = new System.IO.FileStream( strFilePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write );
   System.IO.StreamWriter writer = new System.IO.StreamWriter(fs);
   writer.BaseStream.Seek(0, System.IO.SeekOrigin.End);
   writer.Write("Log Entry : ");
   writer.Write("{0} {1} \n\n", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
   writer.Write(strInput + ";");
   writer.WriteLine();
   writer.Flush();
   writer.Close(); 
  }

  #region ---- DataBase Operation ----------------------------------------

  public bool ExecuteSP(string sp_name,string[] Parameters ,string[] ParameterValues,string[] ParameterTypes )
  {
   try
   {
    return ExecuteSP( sp_name, Parameters, ParameterValues, ParameterTypes, ePO_CONNECTSTRING );
   }
   catch( System.Exception err )
   {
    throw new Exception ( err.Message, err );
   }
  }

  private bool ExecuteSP(string sp_name, string[] Parameters, string[] ParameterValues, string[] ParameterTypes, string connStr )
  {
   try
   {
    this.myConn = new SqlConnection( connStr );
    this.myCommand= new SqlCommand( sp_name, this.myConn );
    this.myCommand.CommandType = System.Data.CommandType.StoredProcedure;
    SqlParameter sqlPara=null;
    SqlParameter Return_Parm = this.myCommand.Parameters.Add( "RETURN_VALUE", System.Data.SqlDbType.Int );
    Return_Parm.Direction = ParameterDirection.ReturnValue;

    for(int i=0; i<Parameters.Length; i++)
    {
     sqlPara=myCommand.Parameters.Add ("@" + Parameters[i], getSqlType(ParameterTypes[i]));
     sqlPara.Value = getSqlValue( ParameterTypes[i], ParameterValues[i] );
    }

    myConn.Open();
    myCommand.ExecuteNonQuery();
    if( Convert.ToInt32( this.myCommand.Parameters["RETURN_VALUE"].Value ) == 0 )
    {
     return true; // 執行成功
    }
    return false; // 執行失敗
   }
   catch( System.Exception err )
   {
    SaveLog( err.Message );
    return false;
   }
   finally
   {
    this.myConn.Close();
   }
  }

  private object getSqlValue( string strType, string strValue )
  {
   switch( strType.ToLower () )
   {
    case "string" :
     return strValue.Trim();

    case "int" :
     return Convert.ToInt32( strValue.Trim() );

    case "bit":
     return Convert.ToByte( strValue.Trim() );

    case "datetime" :
     return Convert.ToDateTime( strValue.Trim() );

    case "bigint" :
     return Convert.ToInt64( strValue.Trim() );

    case "tinyint":
     return Convert.ToByte( strValue.Trim() );

    case "money" :
     return Convert.ToDecimal( strValue.Trim() );

    case "smalldatetime" :
     return Convert.ToDateTime( strValue.Trim() );

    default :
     return strValue.Trim();
   }
  }

  private SqlDbType getSqlType(string strType)
  {
   switch(strType.ToLower ())
   {
    case "string" :
     return SqlDbType.NVarChar;
    case "int" :
     return SqlDbType.Int ;
    case "bit":
     return SqlDbType.Bit  ;
    case "datetime" :
     return SqlDbType.DateTime  ;
    case "bigint" :
     return SqlDbType.BigInt  ;
    case "tinyint":
     return SqlDbType.TinyInt;
    case "money" :
     return SqlDbType.Money   ;
    case "smalldatetime" :
     return SqlDbType.SmallDateTime  ;
    default :
     return SqlDbType.NVarChar ;
   }
  }

posted on 2009-01-05 15:21  ChinaLeo  阅读(200)  评论(0)    收藏  举报

导航