2013-03-20 121 views
0
獲取最新信息

嗨我試圖從一個表中使用LinqToSQL獲取最大時間日期。我已經提出了類似的問題,但是試圖做一些更具體的事情,沒有人能夠提供幫助。這一次,我不介意使用任何解決方案,在SSIS包的另一部分缺少執行SQL任務。這是我的整個代碼。我只是想獲得標識列,它甚至沒有工作,所以請忽略ToString,如果它看起來不合適。我只是想獲得最新的CREATED_TIMESTAMP請linqToSQL datetime從表

using System; 
using System.Data; 
using Microsoft.SqlServer.Dts.Runtime; 
using System.Windows.Forms; 
using System.IO; 
using System.Linq; 
using System.Data.SqlClient; 
using System.Linq.Expressions; 
using System.Collections; 
using System.Collections.Generic; 
using System.Data.Linq.Mapping; 
using System.Data.Linq; 



namespace ST_663004ffff194a14b84e2291578ada33.csproj 
{ 
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")] 
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase 
    { 

     #region VSTA generated code 
     enum ScriptResults 
     { 
      Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, 
      Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure 
     }; 
     #endregion 


      //Strings for connections 
      string iFileName; 
      string oFileName; 
      string RW; 


     public void Main() 
     { 
      Dts.Variables["latestTableRow"].Value = getLatest(); 

      MessageBox.Show(getLatest()); 

      Dts.TaskResult = (int)ScriptResults.Success; 

     }// End Main 

     public string getLatest() 
     { 
      string result = ""; 

      ////temp dummy/defaul date is two days ago 
      //DateTime result = new DateTime(); 
      //result = DateTime.Now.AddDays(-2); 

      try 
      { 
       //get the data connection string from the connection manager 
       RW = (string)Dts.Connections["ReportingWarehouse"].ConnectionString; 

       //Remove the Provider, Auto Translate, and Application 
       //as it is not a parameter for the DataContext constructor 
       RW = RW.Remove(RW.IndexOf("Provider=SQLNCLI10.1;"), "Provider=SQLNCLI10.1;".Length); 
       RW = RW.Remove(RW.IndexOf("Auto Translate=False;"), "Provider=SQLNCLI10.1;".Length); 
       RW = RW.Remove(RW.IndexOf("Application"),RW.Length - RW.IndexOf("Application")); 



       MessageBox.Show(RW); 

       //get the last insertion date from the SSASLoging table 
       using (DataContext RWData = new DataContext(RW)) 
       { 

        Table<SSASLogging> records = RWData.GetTable<SSASLogging>(); 
        var rs = (from r in records 
           orderby r.TimeStamp descending 
           select r).Max(); 
        result = rs.Errorval.ToString(); 
       } 

       MessageBox.Show(result); 

      } 

      catch (Exception e) 
      { 
       MessageBox.Show("Exception in retreiving latest time: " + e.Message + "/n" 
           + e.StackTrace); 
      } 

      return result; 
     } 

    }//end partial class 


    [Table(Name = "SSASLogging")] 
    public class SSASLogging 
    { 

     private DateTime timeStamp; 

     [Column(Name = "CREATED_TIMESTAMP")] 
     public DateTime TimeStamp 
     { 
      get { return this.timeStamp; } 
      private set { ;} 
     } 
    }//End SSASLogging 
} 

回答

0

好這部作品

  using (DataContext RWData = new DataContext(RW)) 
      { 
       Table<SSASLogging> records = RWData.GetTable<SSASLogging>(); 
       var rs = (from r in records 
          select r).Max(r => r.TimeStamp); 
       result = rs; 
      }