Code for exporting excel data to sql server2000
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Text;
protected void btnExport_Click(object sender, EventArgs e)
{
OdbcConnection connection;
SqlBulkCopy bulkCopy;
string ConnectionString = @"server=192.14.45.135;database= NitDB; User ID= sa; Password=gis";
string connstr = @"Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" + fyle.Value;
//"fyle" is the html file upload control used in asp.net
//"fyle.value" returns the path of the file selected.
using (connection = new OdbcConnection(connstr))
{
string[] temp;
string fn1;
fn1 = System.IO.Path.GetFileName(fyle.PostedFile.FileName);
temp = fn1.Split('.');
OdbcCommand command = new OdbcCommand("Select * FROM [" + temp[0].ToString() + "$]", connection);
connection.Open();
using (OdbcDataReader dr = command.ExecuteReader())
{
using (bulkCopy = new SqlBulkCopy(ConnectionString))
{
bulkCopy.DestinationTableName = "MT";
bulkCopy.WriteToServer(dr);
}
dr.Close();
}
}
bulkCopy.Close();
connection.Close();
}
}
This post has been edited by nitin.raj: 21 October 2008 - 01:42 AM

New Topic/Question
This topic is locked




MultiQuote


|