Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I have an Windows Forms Application in C Sharp(C#) which will Connect to SQL and Execute an SQL Query every 5 Seconds and gives the output in a Message Box whenever the latest Executed Value is different from Old Executed Value.

Now i need to Export the data which is displayed(When value changes) in the Message box Should be copied to an Excel Sheet.


The code which i have is :-

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace LocationFinder
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
                String old_value = "";
                while (true)
                {
                    String connetionString = null;
                    SqlConnection conn;
                    SqlCommand cmd;
                    String sql = null;
                    SqlDataReader reader;

                    connetionString = "server=(local);database=modelDb;user id=sa;pwd=123456";
                    sql = "DECLARE @var varchar(1000) = (SELECT TOP 1 Text FROM Alarms WHERE AlarmDefinitionId=139 ORDER BY EventTime DESC) DECLARE @start_position int, @end_position int SELECT @start_position = PATINDEX('% at%', @var) SELECT @end_position = PATINDEX('%kilometers%', @var) DECLARE @VALUE VARCHAR(10) = (Select SUBSTRING(@var, @start_position+5,5)) Select Top 1 @VALUE,RouteTable.Latitude,Routetable.Longitude,Alarms.ApplicationTime FROM Alarms INNER JOIN Routetable ON Routetable.Location BETWEEN FLOOR(@VALUE)-1 AND CEILING(@VALUE)+1 WHERE AlarmDefinitionId=139 ORDER BY EventTime DESC";

                    conn = new SqlConnection(connetionString);
                    try
                    {
                        conn.Open();
                        cmd = new SqlCommand(sql, conn);
                        reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {

                            if (old_value.ToString() != reader.GetValue(0).ToString())
                            {
                                MessageBox.Show("Leak Location" + " |    " + "Latitude" + "     |  " + "Longitude" + "    | " + "Leak Occured Time" + Environment.NewLine + "        " + reader.GetValue(0) + "         | " + reader.GetValue(1) + "  |  " + reader.GetValue(2) + " |   " + reader.GetValue(3));
                            }
                            else
                            {
                             
                            }
                            old_value = reader.GetValue(0).ToString();
                       }
                        reader.Close();
                        cmd.Dispose();
                        conn.Close();
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Cannot Open Connection...! ");
                    }
                    System.Threading.Thread.Sleep(5 * 1000);
                }
            }
 
     private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About_LeakLocationFinder formhelp = new About_LeakLocationFinder();
            formhelp.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
<pre lang="c#">
Posted

1 solution

Try this link[^] or this link[^]

Hope it helps!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900