Quantcast
Channel: CodeProject Latest postings for ASP.NET
Viewing all articles
Browse latest Browse all 3938

My service move my file Location but doesn't upload the data in sql server... here is my code

$
0
0
<pre>using System;
using System.ServiceProcess;
using System.Data.SqlClient;
using System.IO;
using System.Timers;
using System.Configuration;
using System.Data.OleDb;
using System.Data;
 

 

namespace MoveToDesiredDest
{
    publicpartialclass Service1 : ServiceBase
    {
        long delay = 5000;
        protectedstring LogPath = ConfigurationManager.AppSettings["LogPath"];
        protectedstring MoveFilePathPath = ConfigurationManager.AppSettings["MovePath"];
 
        public Service1()
        {
            InitializeComponent();
 

            Timer timer1 = new Timer();
            timer1.Elapsed += new ElapsedEventHandler(OnElapsedTime);
            timer1.Interval = delay;
            timer1.Enabled = true;
            timer1.Start();
        }
 
        protectedoverridevoid OnStart(string[] args)
        {
            WriteLog("Service started");
 

            if (!Directory.Exists(LogPath))
            {
                Directory.CreateDirectory(LogPath);
            }
 
            try
            {
 
                delay = Int32.Parse(ConfigurationManager.AppSettings["IntervalInSeconds"]) * 1000;
            }
            catch
            {
                WriteLog("IntervalInSeconds key/value incorrect.");
            }
 

            if (delay <5000)
            {
                WriteLog("Sleep time too short: Changed to default(5 secs).");
                delay = 5000;
            }
 
            //Timer timer1 = new Timer();//timer1.Elapsed += new ElapsedEventHandler(OnElapsedTime);//timer1.Interval = delay;//timer1.Enabled = true; 

        }
 
        privatevoid OnElapsedTime(object source, ElapsedEventArgs e)
        {
            write();
            Upload();
        }
      
        privatevoid write() 
        {
 
            string sourcepath = ConfigurationManager.AppSettings["sourcepath"];
            WriteLog("Set source path");
            string[] sourcefiles = Directory.GetFiles(sourcepath);
            WriteLog("Get soutce path file");
 
            foreach (string childfile in sourcefiles)
            {
                WriteLog("Start to find file from loop");
                string sourceFileName = new FileInfo(childfile).Name;
                string destinationPath = ConfigurationManager.AppSettings["destinationPath"];
 
                string destinationFileName = sourceFileName;
                string sourceFile = Path.Combine(sourcepath, sourceFileName);
                WriteLog("Get file from source to destination");
                string destinationFile = Path.Combine(destinationPath, destinationFileName);
                WriteLog("Ready to copy");
                File.Copy(sourceFile, destinationFile, true);
                WriteLog("File copied");
                File.Delete(sourceFile);
                WriteLog("File deleted");
            }
        }
        protectedvoid Upload()
        {
            string excelPath = ConfigurationManager.AppSettings["ExcelPath"];
            DirectoryInfo d = new DirectoryInfo(excelPath);
            FileInfo[] Files = d.GetFiles("*.xls");
 

 
            string str = "";
            WriteLog("ready to enter into loop");
            foreach (FileInfo file in Files)
            {
                str = file.Name;
                string sourceFilePath = ConfigurationManager.AppSettings["SourceFilePath"];
                string SourceFileName = Path.Combine(sourceFilePath, str);
 
                string destinationFilePath = ConfigurationManager.AppSettings["MovePath"];
 
                string destinationFileName = destinationFilePath + str;
 

 

                WriteLog("get file :" + str);
                String strConnection = ConfigurationManager.AppSettings["constr"];
                WriteLog("declare database connection");
                string path = excelPath + str;
 
                // string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 8.0;HDR =YES;Persist Security Info=False";string excelConnectionString = @"Microsoft.Jet.OLEDB.4.0;Data Source=" + path +";Extended Properties=Excel 8.0;HDR =YES;Persist Security Info=False";
                WriteLog("declare excel oledb connection");
                OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
 
                OleDbCommand cmd = new OleDbCommand("Select [ID],[Name] from [Sheet1$]", excelConnection);
 
                    //("Select [Department No],[Department],[Emp No],[Name],[Date],[First],[Last] from [Sheet1$]", excelConnection); 
                excelConnection.Open();
                WriteLog("oledb open");
                OleDbDataReader dReader;
 
                dReader = cmd.ExecuteReader();
 
                SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
 
                sqlBulk.DestinationTableName = "Test1";
                WriteLog("ready to insert into database");
                sqlBulk.WriteToServer(dReader);
                WriteLog("data inserted");
                excelConnection.Close();
                WriteLog("Process completed");
                File.Move(SourceFileName, destinationFileName);
                WriteLog("File moved to bak folder");
                excelConnection.Close();
                cmd.Dispose();
            }
        }
 
       
        protectedoverridevoid OnStop()
        {
            WriteLog("service stopped");
        }
 

        protectedvoid WriteLog(string Msg)
        {
            FileStream fs = new FileStream(LogPath + "\\" + System.DateTime.Today.ToString("yyyyMMdd") + ".log", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.BaseStream.Seek(0, SeekOrigin.End);
            sw.WriteLine(System.DateTime.Now.ToString() + ": " + Msg);
            sw.Close();
            sw.Dispose();
            fs.Close();
            fs.Dispose();
        }
    }
}

Viewing all articles
Browse latest Browse all 3938

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>