here is some code from a console app that i did a while ago. Hope it helps
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using ImportExcel.Properties;
namespace ImportExcel
{
internal class Program
{
private static void Main()
{
try
{
if (File.Exists(Settings.Default.filename))
{
DeleteFromSql();
ReadExcel();
}
else
{
Console.WriteLine("STATUS: File not found.\r\nACTION: No action taken.\r\n");
Console.WriteLine(" ");
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// Reads the excel spread sheet.
/// </summary>
private static void ReadExcel()
{
try
{
//get the file path and name from app.config
string filename = Settings.Default.filename;
//create the connection string
string connectionString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", filename);
using (var dataAdapter = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}$]", Settings.Default.SheetName), connectionString))
{
using (var myDataSet = new DataSet())
{
dataAdapter.Fill(myDataSet, "ExcelInfo");
DataTable dataTable = myDataSet.Tables["ExcelInfo"];
List<DataRow> query = (from r in dataTable.AsEnumerable() select r).ToList();
foreach (DataRow item in query)
{
//check for values by length
if ((item.ItemArray[0].ToString().Length == 0) || (item.ItemArray[1].ToString().Length == 0))
continue;
//let the user see something on the screen.
Console.WriteLine("Working...");
//lets write to the db.
SaveToSqldb(item.ItemArray[0].ToString(), item.ItemArray[1].ToString(), item.ItemArray[2].ToString(), item.ItemArray[3].ToString(), item.ItemArray[4].ToString(), item.ItemArray[5].ToString());
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//used to pause so the user can see the error.
Console.WriteLine(" ");
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
}
}
/// <summary>
/// Inserts to SQLDB.
/// </summary>
public static void SaveToSqldb(params string[] listof)
{
string strConn = Settings.Default.connect;
using (var cxn = new SqlConnection(strConn))
{
var command = new SqlCommand("INSERT INTO ws_tblCycle (JobNumber,PKGNumber,Client, Postage,Class,Quantity) VALUES ( @JobNumber,@PKGNumber,@Client,@Postage,@Class,@Quantity)", cxn);
var param0 = new SqlParameter("@JobNumber", SqlDbType.NVarChar, 5) { Value = listof[0] };
command.Parameters.Add(param0);
var param1 = new SqlParameter("@PKGNumber", SqlDbType.NVarChar, 5) { Value = listof[1] };
command.Parameters.Add(param1);
var param2 = new SqlParameter("@Client", SqlDbType.NVarChar, 50) { Value = listof[2] };
command.Parameters.Add(param2);
var param3 = new SqlParameter("@Postage", SqlDbType.NVarChar, 50) { Value = listof[3] };
command.Parameters.Add(param3);
var param4 = new SqlParameter("@Class", SqlDbType.NVarChar, 50) { Value = listof[4] };
command.Parameters.Add(param4);
var param5 = new SqlParameter("@Quantity", SqlDbType.BigInt) { Value = listof[5] };
command.Parameters.Add(param5);
cxn.Open();
command.ExecuteNonQuery();
cxn.Close();
}
}
/// <summary>
/// Deletes from SQL.
/// </summary>
public static void DeleteFromSql()
{
string strConn = Settings.Default.connect;
using (var cxn = new SqlConnection(strConn))
{
var command = new SqlCommand("DELETE FROM ws_tblCycle", cxn);
cxn.Open();
command.ExecuteNonQuery();
cxn.Close();
}
}
}
}
cornboy88
Member
217 Points
40 Posts
Re: Import contents of excel file to SQL table
Feb 23, 2012 04:56 PM|LINK
here is some code from a console app that i did a while ago. Hope it helps
using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; using System.IO; using System.Linq; using ImportExcel.Properties; namespace ImportExcel { internal class Program { private static void Main() { try { if (File.Exists(Settings.Default.filename)) { DeleteFromSql(); ReadExcel(); } else { Console.WriteLine("STATUS: File not found.\r\nACTION: No action taken.\r\n"); Console.WriteLine(" "); Console.WriteLine("Press any key to continue..."); Console.ReadKey(true); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } /// <summary> /// Reads the excel spread sheet. /// </summary> private static void ReadExcel() { try { //get the file path and name from app.config string filename = Settings.Default.filename; //create the connection string string connectionString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", filename); using (var dataAdapter = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}$]", Settings.Default.SheetName), connectionString)) { using (var myDataSet = new DataSet()) { dataAdapter.Fill(myDataSet, "ExcelInfo"); DataTable dataTable = myDataSet.Tables["ExcelInfo"]; List<DataRow> query = (from r in dataTable.AsEnumerable() select r).ToList(); foreach (DataRow item in query) { //check for values by length if ((item.ItemArray[0].ToString().Length == 0) || (item.ItemArray[1].ToString().Length == 0)) continue; //let the user see something on the screen. Console.WriteLine("Working..."); //lets write to the db. SaveToSqldb(item.ItemArray[0].ToString(), item.ItemArray[1].ToString(), item.ItemArray[2].ToString(), item.ItemArray[3].ToString(), item.ItemArray[4].ToString(), item.ItemArray[5].ToString()); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); //used to pause so the user can see the error. Console.WriteLine(" "); Console.WriteLine("Press any key to continue..."); Console.ReadKey(true); } } /// <summary> /// Inserts to SQLDB. /// </summary> public static void SaveToSqldb(params string[] listof) { string strConn = Settings.Default.connect; using (var cxn = new SqlConnection(strConn)) { var command = new SqlCommand("INSERT INTO ws_tblCycle (JobNumber,PKGNumber,Client, Postage,Class,Quantity) VALUES ( @JobNumber,@PKGNumber,@Client,@Postage,@Class,@Quantity)", cxn); var param0 = new SqlParameter("@JobNumber", SqlDbType.NVarChar, 5) { Value = listof[0] }; command.Parameters.Add(param0); var param1 = new SqlParameter("@PKGNumber", SqlDbType.NVarChar, 5) { Value = listof[1] }; command.Parameters.Add(param1); var param2 = new SqlParameter("@Client", SqlDbType.NVarChar, 50) { Value = listof[2] }; command.Parameters.Add(param2); var param3 = new SqlParameter("@Postage", SqlDbType.NVarChar, 50) { Value = listof[3] }; command.Parameters.Add(param3); var param4 = new SqlParameter("@Class", SqlDbType.NVarChar, 50) { Value = listof[4] }; command.Parameters.Add(param4); var param5 = new SqlParameter("@Quantity", SqlDbType.BigInt) { Value = listof[5] }; command.Parameters.Add(param5); cxn.Open(); command.ExecuteNonQuery(); cxn.Close(); } } /// <summary> /// Deletes from SQL. /// </summary> public static void DeleteFromSql() { string strConn = Settings.Default.connect; using (var cxn = new SqlConnection(strConn)) { var command = new SqlCommand("DELETE FROM ws_tblCycle", cxn); cxn.Open(); command.ExecuteNonQuery(); cxn.Close(); } } } }