pgupta81
asked on
load a db table from excel
I am new to .net and i wanted to know how to implemnet thisi project that i have been given and wanted an experts advice on the best way to implement the project.Plz advice.
Requiremnets:
Read from multiple sheets of an excel file and load the single sql db table with this data from the excel file and also run a stored procedure in the background
to manupilate other db tables using this table.
This is an windows application.
Thank you.
Requiremnets:
Read from multiple sheets of an excel file and load the single sql db table with this data from the excel file and also run a stored procedure in the background
to manupilate other db tables using this table.
This is an windows application.
Thank you.
IF it is not a One time requirement then
On some Button Click Event (Or whatever is the UI event)...You could probably do something like this
Function LoadDataFrom ExcelAndUpdateDB()
{
// Write your Code for filling some dataset from the Excel file
// This Could return a DataSet
foreach(DataRow dr in DataSet.Tables[0].Rows)
{
//Write your Code to take all the rows from the datatable and call your SP
}
}
On some Button Click Event (Or whatever is the UI event)...You could probably do something like this
Function LoadDataFrom ExcelAndUpdateDB()
{
// Write your Code for filling some dataset from the Excel file
// This Could return a DataSet
foreach(DataRow dr in DataSet.Tables[0].Rows)
{
//Write your Code to take all the rows from the datatable and call your SP
}
}
ASKER
I have to develop a windows application on this thats why i cannot use EM DTS to do this..
How to iterate through multiple sheets of excel to load sql table?
Do i need to follow certain naming convention ?
How to iterate through multiple sheets of excel to load sql table?
Do i need to follow certain naming convention ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Dim DS As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDat aAdapter
Dim MyConnection As System.Data.OleDb.OleDbCon nection
MyConnection = New System.Data.OleDb.OleDbCon nection( _
"provider=Microsoft.Jet.OL EDB.4.0; " & _
"data source=C:\myData.XLS; " & _
"Extended Properties=Excel 8.0;")
' Select the data from Sheet1 of the workbook.
MyCommand = New System.Data.OleDb.OleDbDat aAdapter( _
"select * from [Sheet1$]", MyConnection)
DS = New System.Data.DataSet()
MyCommand.Fill(DS)
MyConnection.Close()
Dim MyCommand As System.Data.OleDb.OleDbDat
Dim MyConnection As System.Data.OleDb.OleDbCon
MyConnection = New System.Data.OleDb.OleDbCon
"provider=Microsoft.Jet.OL
"data source=C:\myData.XLS; " & _
"Extended Properties=Excel 8.0;")
' Select the data from Sheet1 of the workbook.
MyCommand = New System.Data.OleDb.OleDbDat
"select * from [Sheet1$]", MyConnection)
DS = New System.Data.DataSet()
MyCommand.Fill(DS)
MyConnection.Close()
Forgot to submit the function for retrieving data from a given sheet.
public DataTable GetData(string FilePath, string SheetName, bool FirstRowIsHeader)
{
string connString = null;
if (FirstRowIsHeader) {
connString = "Provider=Microsoft.Jet.OL EDB.4.0;" +
"Data Source=" + FilePath + ";" +
"Extended Properties=\"Excel 8.0;HDR=YES\"";
} else {
connString = "Provider=Microsoft.Jet.OL EDB.4.0;" +
"Data Source=" + FilePath + ";" +
"Extended Properties=\"Excel 8.0;HDR=NO\"";
}
OleDbConnection conn = null;
OleDbCommand cmd = new OleDbCommand();
DataTable dt = null;
IDataReader dr = null;
try {
using (conn = new OleDbConnection(connString )) {
if (conn.State != ConnectionState.Open) { conn.Open(); }
cmd.Connection = conn;
cmd.CommandText = "SELECT * FROM [" + SheetName + "$]";
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = cmd;
dt = new DataTable(SheetName);
adapter.FillSchema(dt, SchemaType.Source);
adapter.Fill(dt);
}
} finally {
if (cmd != null) {
cmd.Dispose();
cmd = null;
}
if (dr != null) {
dr.Close();
dr.Dispose();
dr = null;
}
if (conn != null) {
try {
conn.Close();
conn.Dispose();
conn = null;
} catch (OleDbException exOleDb) {
Logger.LogException(exOleD b);
}
}
}
return dt;
}
public DataTable GetData(string FilePath, string SheetName, bool FirstRowIsHeader)
{
string connString = null;
if (FirstRowIsHeader) {
connString = "Provider=Microsoft.Jet.OL
"Data Source=" + FilePath + ";" +
"Extended Properties=\"Excel 8.0;HDR=YES\"";
} else {
connString = "Provider=Microsoft.Jet.OL
"Data Source=" + FilePath + ";" +
"Extended Properties=\"Excel 8.0;HDR=NO\"";
}
OleDbConnection conn = null;
OleDbCommand cmd = new OleDbCommand();
DataTable dt = null;
IDataReader dr = null;
try {
using (conn = new OleDbConnection(connString
if (conn.State != ConnectionState.Open) { conn.Open(); }
cmd.Connection = conn;
cmd.CommandText = "SELECT * FROM [" + SheetName + "$]";
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = cmd;
dt = new DataTable(SheetName);
adapter.FillSchema(dt, SchemaType.Source);
adapter.Fill(dt);
}
} finally {
if (cmd != null) {
cmd.Dispose();
cmd = null;
}
if (dr != null) {
dr.Close();
dr.Dispose();
dr = null;
}
if (conn != null) {
try {
conn.Close();
conn.Dispose();
conn = null;
} catch (OleDbException exOleDb) {
Logger.LogException(exOleD
}
}
}
return dt;
}
ASKER
code Helped!! Thanks
In EM , rt click DATABSE, all tasks - Import and there u give source and destination, Ur source will be excel file