linqToSQL datetime从表中获取最新数据

本文关键字:获取 最新 数据 datetime linqToSQL | 更新日期: 2023-09-27 18:04:49

嗨,我试图使用LinqToSQL从表中获取最大时间。我已经问过一个类似的问题,但我想做一些更具体的事情,没有人能帮上忙。这一次,我不介意使用任何解决方案,除了在SSIS包的另一部分执行SQL任务。这是我的全部代码。我试图只是得到身份列,它甚至没有工作,所以忽略ToString请,如果它似乎不合适。我只是想得到最新的CREATED_TIMESTAMP请

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
}

linqToSQL datetime从表中获取最新数据

可以了

            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;
            }