Guest User

Untitled

a guest
Jun 13th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data.SqlClient;
  7. using System.Data;
  8. using System.Data.OleDb;
  9. using System.Data.Common;
  10.  
  11.  
  12. namespace open_and_send_to_SQL
  13. {
  14.     class ReadFromFile
  15.     {
  16.  
  17.         static void Main()
  18.         {
  19.             try
  20.             {
  21.                 // Create the connection to the Database
  22.                 using (SqlConnection conn = new SqlConnection())
  23.                 {
  24.                     // Create the connectionString
  25.                     // Trusted_Connection is used to denote the connection uses Windows Authentication
  26.                     conn.ConnectionString = "Server=LT06-TOM;Database=IrishNHS;User Id=sa;Password =THE59dhYu2ye";
  27.                     //Server=LT06-TOM;Database=IrishNHS;User Id=sa;Password =THE59dhYu2ye;
  28.  
  29.                     // Read each line of the file into a string array. Each element of the array is one line of the file.
  30.                     string[] lines = System.IO.File.ReadAllLines(@"C:\WatchFolder\qa\StaffChanges_20180306.xlsx");
  31.  
  32.                     //Save File as Temp then you can delete it if you want
  33.                     var path = (@"C:\WatchFolder\qa\StaffChanges_20180306.xlsx");
  34.                     //Save File as Temp then you can delete it if you want
  35.  
  36.                     //FileUpload1.SaveAs(path);
  37.                     //string path = @"C:\WatchFolder\WriteLines2.txt";
  38.  
  39.                     string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0", path);
  40.  
  41.                     // Create Connection to Excel Workbook
  42.                     using (OleDbConnection connection =
  43.                                  new OleDbConnection(excelConnectionString))
  44.                     {
  45.                         OleDbCommand command = new OleDbCommand
  46.                                 ("Select * FROM [Sheet1$]", connection);
  47.  
  48.                         connection.Open();
  49.  
  50.                         // Create DbDataReader to Data Worksheet
  51.                         using (DbDataReader dr = command.ExecuteReader())
  52.                         {
  53.  
  54.                             // SQL Server Connection String
  55.                             string sqlConnectionString = @"Server=LT06-TOM;Database=IrishNHS;User Id=sa;Password =THE59dhYu2ye";
  56.                            
  57.                             // Bulk Copy to SQL Server
  58.                             using (SqlBulkCopy bulkCopy =
  59.                                        new SqlBulkCopy(sqlConnectionString))
  60.                             {
  61.                                 bulkCopy.DestinationTableName = "VSImportTable";
  62.                                 bulkCopy.WriteToServer(dr);
  63.                                 Console.WriteLine("The data has been exported succefuly from Excel to SQL");    
  64.                             }
  65.                         }
  66.                     }
  67.                 }
  68.             }
  69.  
  70.             catch (Exception ex)
  71.             {
  72.                 Console.WriteLine(ex.Message);
  73.             }
  74.        
  75.                    
  76.  
  77.  
  78.                 // Keep the console window open in debug mode
  79.                 Console.WriteLine("Press any key to exit.");
  80.                 System.Console.ReadKey();
  81.             }
  82.         }
  83.     }
Advertisement
Add Comment
Please, Sign In to add comment