Reading Data from XLSX in C# using ExcelDataReader v2.1 Library and writing to SQL Server.
I need to write a console application in C# reading data from XLSX and writing to SQL server using ExcelDataReader v2.1 Library .
I need to read the data in Cell D17,D18,D19 and drop to buying column in SQL.
And read Cell K17,K18,K19 and drop to /selling column in SQL
This is the SQL table query and database name is test1
I need to read the data in Cell D17,D18,D19 and drop to buying column in SQL.
And read Cell K17,K18,K19 and drop to /selling column in SQL
This is the SQL table query and database name is test1
CSS
SELECT TOP 1000 [id]
,[code]
,[buying]
,[selling]
FROM [test1].[dbo].[Currency]C#
This is a code i tried but it is not using ExcelDataReader v2.1. i need help to do it with ExcelDataReader v2.1 Collapse | Copy Code
using System;
using System.IO;
using Bytescout.Spreadsheet;
using System.Data.SqlClient;
namespace ExportToSQLServer
{
class Program
{
static void Main(string[] args)
{
try
{
string connectionString = "Data Source=192.168.1.215;Initial Catalog=RECIPES2;Persist Security Info=True;User ID=sa;Password=***********";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
ExecuteQueryWithoutResult(connection, "USE test1");
//Load XLS document
using (Spreadsheet document = new Spreadsheet())
{
document.LoadFromFile("SimpleReport.xls");
Worksheet worksheet = document.Workbook.Worksheets[0];
for (int row = 0; row String insertCommand = string.Format("INSERT XlsTest VALUES('{0}','{1}')",
worksheet.Cell(row, 0).Value, worksheet.Cell(row, 1).Value);
ExecuteQueryWithoutResult(connection, insertCommand);
}
}
// Check the data successfully exported
using (SqlCommand command = new SqlCommand("SELECT * from XlsTest", connection))
{
SqlDataReader reader = command.ExecuteReader();
if (reader != null)
{
Console.WriteLine();
Console.WriteLine("Exported XLS data:");
Console.WriteLine();
while (reader.Read())
{
Console.WriteLine(String.Format("{0} | {1}", reader[0], reader[1]));
}
}
}
Console.WriteLine();
Console.WriteLine("Press any key.");
Console.ReadKey();
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.ReadKey();
}
}
static void ExecuteQueryWithoutResult(SqlConnection connection, string query)
{
using (SqlCommand command = new SqlCommand(query, connection))
{
command.ExecuteNonQuery();
}
}
}
}
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 23, 2013, 12:29 AM EDT
Regards,
Iftikar
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 22, 2013, 11:25 PM EDT
Try like this
Regards,
Iftikar
afshan dc13 reputationPosted Jul 22, 2013, 11:32 PM EDT
afshan dc13 reputationPosted Jul 23, 2013, 12:48 AM EDT
I did it using this method! it reads the whole column...
I need help writing this to the sql database.
using System;
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 23, 2013, 12:58 AM EDT
Regards,
Iftikar
afshan dc13 reputationPosted Jul 23, 2013, 2:27 AM EDT
How do i loop this and read all the values in the column D starting from Cell 17
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 23, 2013, 2:31 AM EDT
Regards,
Iftikar
afshan dc13 reputationPosted Jul 23, 2013, 2:41 AM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 23, 2013, 2:43 AM EDT
Regards,
Iftikar
afshan dc13 reputationPosted Jul 23, 2013, 2:44 AM EDT
afshan dc13 reputationPosted Jul 23, 2013, 2:58 AM EDT
And how do i write this to the SQL database??? Sorry for the trouble! :(
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 23, 2013, 3:10 AM EDT
Regards,
Iftikar