i am preparing myself for importing data from excel sheet to sql server 2005.I have few requirements while i import data from excel sheet to database.I have searched a lot but i am unable to find the answer and also i am getting confused,so thought of asking my query over here.The excel sheet format is in this way.
First row contains Heading "Transaction"
Second row contains SubHeading "Date"
From third row the data starts with column names followed by the data in resp.rows.
CustId CustName OUtlet TerminalNum Date DateTime Amount<br/>
1 Nazima SS 770012234 1/22/2011 1/22/2011 12:34:45 1500.50<br/>
this is the way the data is in excel sheet.
i have tried to upload the image but it was showing problem while posting the question.so i typed it.
My requirement is that when i import data from excel sheet to database i have to ignore the first two rows and have to import data from third row and i have to import totalamount(62854) and total no.of rows(11) in one table and data in other table.
I am able to import the data using oledbcommand by using this statement:
oledbcommand cmd=new oledbcommand("select * from [sheet1$]",con);
and
oledbcomaand cmd=new oledbcommand("select * from [sheet1$A3:H13]",con);
these two statements are working but i dont want it in this way because enduser can enter the data from any cell,so i want to dynamically specify the sheet along with range.In whichever cell the enduser start entering data,the data along with total amount and no.of rows should get imported to reso.tables in database without any problem.
And also i want to know how can i insert a new row in table whenever the end user enters a new row in excel sheet without inserting the previous rows which are already present in table.
Next i want to know which is the best way to import data from excel to database.Is it thru excel interop or thru Oledb.I have tried using import/export wizard,SSIS,they are working but i have to do it programatically.
Give me few guidelines how do i need to carry out these process.
Hope u understood my doubt.
Using Interop
there will be an easy and better way. However, I never used that and hence don't know how to use that.
My requirement is that when i import data from excel sheet to database i have to ignore the first two rows and have to import data from third row and i have to import totalamount(62854) and total no.of rows(11) in one table and data in other table.
DataSet
using OleDbDataAdapter
DataSet
, i.e do the required operation.DataSet
to your desired Database
EDIT:
Sample Code:
public static void ExcelOperations(string ConnectionString)
{
try
{
DataTable Sheets = new DataTable();
using (OleDbConnection connection = new OleDbConnection(ConnectionString))
{
connection.Open();
//Retrieve the Sheets
Sheets = connection.GetSchema("Tables");
//Display the Sheets
foreach (DataRow sheet in Sheets.Rows)
{
Console.WriteLine(sheet["TABLE_NAME"]);
}
//Take the First Sheet
string firstSheet = Sheets.Rows[0][2].ToString();
//Retrieve contents
DataSet Contents = new DataSet();
using (OleDbDataAdapter adapter = new OleDbDataAdapter("select FirstName,LastName,Email,Mobile from [" + firstSheet + "]", connection))
{
adapter.Fill(Contents, "MyTable");
}
//Display the contents
foreach (DataRow content in Contents.Tables["MyTable"].Rows)
{
Console.WriteLine(content[0] + " | " + content[1] + " | " + content[2] + " | " + content[3]);
}
Console.WriteLine();
//Remove First Row; Note: This is not heading!
Contents.Tables["MyTable"].Rows.RemoveAt(0);
//Since the first row is removed, Second Row becames first row now.
//Clearing the LastName of our First Row.
Contents.Tables["MyTable"].Rows[0][1] = "";
//Displaying the Contents
foreach (DataRow content in Contents.Tables["MyTable"].Rows)
{
Console.WriteLine(content[0] + " | " + content[1] + " | " + content[2] + " | " + content[3]);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}