如何使用删除根据表中的最大日期从 Oracle 表中删除行?

发布于 2024-11-05 22:57:23 字数 4823 浏览 0 评论 0原文

我正在尝试创建一个 SQL 语句,该语句允许我删除表中早于最合适日期的所有行,同时保留最新的行。这是我正在从事的一个项目中使用的。我正在从 MS Access 数据库中获取数据并将该数据拖到 Oracle 表中。该程序正在构建到我在 Visual Studio 2008 C# 中创建的 Windows 服务应用程序中,以便我可以让它定期运行以获取最新信息。

我只需要弄清楚要添加到我的代码中的更多内容:

  1. 找到某种方法从我的 Oracle 表中删除那些旧行

  2. 告诉我的服务每隔一小时或 2 运行一次

  3. 检查连接错误、丢失文件等。

任何有关我的主要问题和其他步骤的帮助将不胜感激。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.IO;
using System.Data.Odbc;

namespace GasReportservice
{
  public partial class GasReportService : ServiceBase
  {
    public GasReportService()
    {
        InitializeComponent();
        if (!System.Diagnostics.EventLog.SourceExists("MySource"))
        {
            System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog");
        }
        eventLog1.Source = "MySource";
        eventLog1.Log = "MyNewLog";
    }
    protected override void OnStart(string[] args)
    {
        eventLog1.WriteEntry("In OnStart");
        string connectionString = "Dsn=Gas_meter";
        string col0 = "";
        string col1 = "";
        string col2 = "";
        string col3 = "";
        string col4 = "";
        string col5 = "";
        string col6 = "";
        string col7 = "";
        string col8 = "";
        OdbcConnection DbConnection = new OdbcConnection(connectionString);
        OdbcCommand DbCommand = DbConnection.CreateCommand();
        DbConnection.Open();
        DbCommand.CommandText = "SELECT DateTime, S1Flow, S2Flow, S3Flow, S4Flow, S1FlowTotal, S2FlowTotal, S3FlowTotal, S4FlowTotal FROM CommonStation WHERE Format(DateTime, 'mm/dd/yyyy') >=(select Format(max(DateTime),'mm/dd/yyyy') from CommonStation)";
        OdbcDataReader DbReader = DbCommand.ExecuteReader();
        int fCount = DbReader.FieldCount;


        /*
        for (int i = 0; i < fCount; i++)
        {
                String fName = DbReader.GetName(i);
                Console.Write(fName + "\t");
        }
        */

        try
        {
            while (DbReader.Read())
            {
                string connString = "DSN=Gas_meter_proj;Uid=cm;Pwd=cmdev123";
                OdbcConnection conn = new OdbcConnection(connString);
                string sqlins = @"insert into Commonstation(CommStatDate_Time, S1_Flow, S2_Flow, S3_Flow, S4_Flow, S1_Flow_Total, S2_Flow_Total, S3_Flow_Total, S4_Flow_Total ) values (to_date('" + col0 + "', 'MM/DD/YYYY HH:MI:SS AM' ),to_number('" + col1 + "'), to_number('" + col2 + "'), to_number('" + col3 + "'), to_number('" + col4 + "'),to_number('" + col5 + "'),to_number('" + col6 + "'),to_number('" + col7 + "'),to_number('" + col8 + "'))";
                OdbcCommand cmdnon = new OdbcCommand(sqlins, conn);
                cmdnon.Parameters.Add(col0, OdbcType.DateTime);
                cmdnon.Parameters.Add(col1, OdbcType.Numeric);
                cmdnon.Parameters.Add(col2, OdbcType.Numeric);
                cmdnon.Parameters.Add(col3, OdbcType.Numeric);
                cmdnon.Parameters.Add(col4, OdbcType.Numeric);
                cmdnon.Parameters.Add(col5, OdbcType.Numeric);
                cmdnon.Parameters.Add(col6, OdbcType.Numeric);
                cmdnon.Parameters.Add(col7, OdbcType.Numeric);
                cmdnon.Parameters.Add(col8, OdbcType.Numeric);
                conn.Open();
                col0 = DbReader["DateTime"].ToString();
                col1 = DbReader["S1Flow"].ToString();
                col2 = DbReader["S2Flow"].ToString();
                col3 = DbReader["S3Flow"].ToString();
                col4 = DbReader["S4Flow"].ToString();
                col5 = DbReader["S1FlowTotal"].ToString();
                col6 = DbReader["S2FlowTotal"].ToString();
                col7 = DbReader["S3FlowTotal"].ToString();
                col8 = DbReader["S4FlowTotal"].ToString();

                int rowsAffected = cmdnon.ExecuteNonQuery();

                conn.Close();
                Console.WriteLine(rowsAffected);                                   
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            DbReader.Close();
            DbCommand.Dispose();
            DbConnection.Close();
        }

    }

    protected override void OnContinue()
    {
        eventLog1.WriteEntry("My service has resumed action.");
    }

    protected override void OnPause()
    {
        eventLog1.WriteEntry("My service has been paused.");
    } 

    protected override void OnStop()
    {
        eventLog1.WriteEntry("My service has been stopped.");
    }
  }
}

I am trying to create a SQL statement that will allow me to remove all rows in a table that is older than the most decent date while keeping the most recent. This is being used for a project that I am working on. I am grabbing data from a MS Access database and dragging that data to an oracle table. This program is being built into a Windows Service application, that I am creating in Visual Studio 2008 C#, so that I can have it run periodically to grab the most recent information.

I just need to figure out a few more things to add to my code:

  1. Find some way to delete those older rows from my Oracle table

  2. Tell my service to run every so often every hour or 2

  3. Check to errors in connections, missing files, etc.

Any help with my primary question and the other steps would be greatly appreciated.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.IO;
using System.Data.Odbc;

namespace GasReportservice
{
  public partial class GasReportService : ServiceBase
  {
    public GasReportService()
    {
        InitializeComponent();
        if (!System.Diagnostics.EventLog.SourceExists("MySource"))
        {
            System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog");
        }
        eventLog1.Source = "MySource";
        eventLog1.Log = "MyNewLog";
    }
    protected override void OnStart(string[] args)
    {
        eventLog1.WriteEntry("In OnStart");
        string connectionString = "Dsn=Gas_meter";
        string col0 = "";
        string col1 = "";
        string col2 = "";
        string col3 = "";
        string col4 = "";
        string col5 = "";
        string col6 = "";
        string col7 = "";
        string col8 = "";
        OdbcConnection DbConnection = new OdbcConnection(connectionString);
        OdbcCommand DbCommand = DbConnection.CreateCommand();
        DbConnection.Open();
        DbCommand.CommandText = "SELECT DateTime, S1Flow, S2Flow, S3Flow, S4Flow, S1FlowTotal, S2FlowTotal, S3FlowTotal, S4FlowTotal FROM CommonStation WHERE Format(DateTime, 'mm/dd/yyyy') >=(select Format(max(DateTime),'mm/dd/yyyy') from CommonStation)";
        OdbcDataReader DbReader = DbCommand.ExecuteReader();
        int fCount = DbReader.FieldCount;


        /*
        for (int i = 0; i < fCount; i++)
        {
                String fName = DbReader.GetName(i);
                Console.Write(fName + "\t");
        }
        */

        try
        {
            while (DbReader.Read())
            {
                string connString = "DSN=Gas_meter_proj;Uid=cm;Pwd=cmdev123";
                OdbcConnection conn = new OdbcConnection(connString);
                string sqlins = @"insert into Commonstation(CommStatDate_Time, S1_Flow, S2_Flow, S3_Flow, S4_Flow, S1_Flow_Total, S2_Flow_Total, S3_Flow_Total, S4_Flow_Total ) values (to_date('" + col0 + "', 'MM/DD/YYYY HH:MI:SS AM' ),to_number('" + col1 + "'), to_number('" + col2 + "'), to_number('" + col3 + "'), to_number('" + col4 + "'),to_number('" + col5 + "'),to_number('" + col6 + "'),to_number('" + col7 + "'),to_number('" + col8 + "'))";
                OdbcCommand cmdnon = new OdbcCommand(sqlins, conn);
                cmdnon.Parameters.Add(col0, OdbcType.DateTime);
                cmdnon.Parameters.Add(col1, OdbcType.Numeric);
                cmdnon.Parameters.Add(col2, OdbcType.Numeric);
                cmdnon.Parameters.Add(col3, OdbcType.Numeric);
                cmdnon.Parameters.Add(col4, OdbcType.Numeric);
                cmdnon.Parameters.Add(col5, OdbcType.Numeric);
                cmdnon.Parameters.Add(col6, OdbcType.Numeric);
                cmdnon.Parameters.Add(col7, OdbcType.Numeric);
                cmdnon.Parameters.Add(col8, OdbcType.Numeric);
                conn.Open();
                col0 = DbReader["DateTime"].ToString();
                col1 = DbReader["S1Flow"].ToString();
                col2 = DbReader["S2Flow"].ToString();
                col3 = DbReader["S3Flow"].ToString();
                col4 = DbReader["S4Flow"].ToString();
                col5 = DbReader["S1FlowTotal"].ToString();
                col6 = DbReader["S2FlowTotal"].ToString();
                col7 = DbReader["S3FlowTotal"].ToString();
                col8 = DbReader["S4FlowTotal"].ToString();

                int rowsAffected = cmdnon.ExecuteNonQuery();

                conn.Close();
                Console.WriteLine(rowsAffected);                                   
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            DbReader.Close();
            DbCommand.Dispose();
            DbConnection.Close();
        }

    }

    protected override void OnContinue()
    {
        eventLog1.WriteEntry("My service has resumed action.");
    }

    protected override void OnPause()
    {
        eventLog1.WriteEntry("My service has been paused.");
    } 

    protected override void OnStop()
    {
        eventLog1.WriteEntry("My service has been stopped.");
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙幽之幻 2024-11-12 22:57:23

这是基本的 SQL。

DELETE FROM <yourtable> 
  WHERE <yourdatefield> < (SELECT Max(<yourdatefield>) FROM <yourtable>)

对于您的其他观点,它们应该是单独的问题。它们是完全独立的主题,应该独立于这个主题来回答(和搜索)。

This is basic SQL.

DELETE FROM <yourtable> 
  WHERE <yourdatefield> < (SELECT Max(<yourdatefield>) FROM <yourtable>)

For your other points, they should be separate questions. They're totally separate topics, and should be answered (and searchable) independently from this one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文