Reputation: 4633
I am using C# Visual Studio 2005 and SQL Server 2005.
Before I implemented Role Management in the ASP.NET Configuration Tool, my import of data from an Excel spreadsheets worked fine. But after I implemented Role Management, every time I tried to import data from my excel spreadsheet, the connection will be forcibly closed.
Below is a code snippet of my excel import:
string strUploadFileName = "C:/Documents and Settings/user01/My Documents/Visual Studio 2005/WebSites/MajorProject/UploadFiles/" + DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;
ExcelImport.SaveAs(strUploadFileName);
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strUploadFileName + ";Extended Properties=Excel 8.0;";
using (OleDbConnection connection = new OleDbConnection(connStr))
{
OleDbCommand command = new OleDbCommand("Select [COLUMNS] FROM [sheet1$]", connection);
connection.Open();
using (DbDataReader dr = command.ExecuteReader())
{
string sqlConnectionString = "Data Source=<DS>";
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "UserData";
bulkCopy.WriteToServer(dr);
}
}
}
Anyone know what might be the possible reasons? I have turned off my role management for my web application and waiting for my server to restart and I will try to import data into my table again.
Thank you in advance for any help.
Upvotes: 0
Views: 257
Reputation: 38
You may need to close the connection when data is read. The using way can only help to dispose the object connection,but not to close it.
Wish it would help.
Upvotes: 1