2013-03-20 82 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; 
      }