i am in the way of developing window based application in C#, sqlserver 2008. ud u advice me w cn i import exel 2007 data to sql server. while i am using the followin code i got the exception like cannot update , database or object is read only
my code is
public void importDataFromExcel(string excelFilePath)
{
//Declare Variables - Edit these based on your particular situation
string sSQLTable = "Branch";
// make sure your sheet name is correct, here sheet name is Sheet1, so you can change your sheet name if have different
string myExcelDataQuery = "Select code,name from [Bran$]";
try
{
//Create our connection strings
string sExcelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath + ";Extended Properties=Excel 8.0";
string sSqlConnectionString = "Initial catalog=accademic;Data Source=.;Integrated Security=true";
//Execute a query to erase any previous data from our destination table
string sClearSQL = "DELETE FROM " + sSQLTable;
//SqlConnection SqlConn = new SqlConnection(sSqlConnectionString);
con = new SqlConnection(sSqlConnectionString);
//con.Open();
SqlCommand SqlCmd = new SqlCommand(sClearSQL, con);
con.Open();
SqlCmd.ExecuteNonQuery();
con.Close();
//Series of commands to bulk copy data from the excel file into our SQL table
OleDbConnection OleDbConn = new OleDbConnection(sExcelConnectionString);
MessageBox.Show ( OleDbConn.State.ToString());
OleDbCommand OleDbCmd = new OleDbCommand(myExcelDataQuery, OleDbConn);
OleDbConn.Open();
OleDbDataReader dr = OleDbCmd.ExecuteReader();
SqlBulkCopy bulkCopy = new SqlBulkCopy(sSqlConnectionString);//
bulkCopy.DestinationTableName = sSQLTable;
while (dr.Read())
{
bulkCopy.WriteToServer(dr);
}
OleDbConn.Close();
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString());
}