query to import excel data to sqlsever

Posted by Parthibansk under Sql Server on 1/31/2013 | Points: 10 | Views : 867 | Status : [Member] | Replies : 2
got a excelsheet. have to import to sqlserver using query..




Responses

Posted by: Shanky11 on: 1/31/2013 [Member] Starter | Points: 25

Up
0
Down
http://www.codeproject.com/Articles/32581/Import-Data-from-Excel-to-SQL-Server

try this link


Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Megan00 on: 1/31/2013 [Member] Starter | Points: 25

Up
0
Down
Use this excel api by below code:
http://spreadsheet.codeplex.com/
            Workbook workbook = new Workbook();

workbook.LoadFromFile(@"..\..\sample.xlsx");
Worksheet sheet = workbook.Worksheets[0];
DataTable data= sheet.ExportDataTable();

string connectionStr = @"Data Source=DEV05\SQLEXPRESS;Initial Catalog=Sample;User Id=amy;Password=123";
SqlConnection conn= new SqlConnection(connectionStr);
SqlCommand command = new SqlCommand();
conn.Open();
command.Connection = conn;
for (int i=1;i<data.Rows.Count;i++)
{
DataRow row=data.Rows[i];

string commd = "insert into [tableSample](Name,Capital,Continent,Area) values('" + row[0].ToString() + "','" + row[1].ToString() +
" ','" + row[2].ToString() + "','" + row[3].ToString()+ "')";
command.CommandText = commd;
command.ExecuteNonQuery();
}
conn.Close();



Never give up! Smile to the world!
http://excelcsharp.blog.com/

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response