请帮助。我在尝试将excel文件上传到数据库时收到此错误。错误发生在这行代码中:
SqlConnection MyCon = new SqlConnection(ConfigurationManager.ConnectionStrings[Excel107ConString].ConnectionString);
以下是我的全部代码。
public partial class WebForm1 : System.Web.UI.Page
{
string Excel107ConString = string.Empty;
protected void Upload(object sender, EventArgs e)
{
string excelPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(excelPath);
string conString = string.Empty;
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
switch (extension)
{
case ".xls":
SqlConnection MyCon = new SqlConnection(ConfigurationManager.ConnectionStrings[Excel107ConString].ConnectionString);
break;
}
conString = string.Format(conString, excelPath);
using (OleDbConnection excel_con = new OleDbConnection(conString))
{
excel_con.Open();
string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
DataTable dtExcelData = new DataTable();
dtExcelData.Columns.AddRange(new DataColumn[3] { new DataColumn("InvoiceNumber", typeof(string)),
new DataColumn("AmountPaid", typeof(decimal)),
new DataColumn("Remarks",typeof(string)) });
using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))
{
oda.Fill(dtExcelData);
}
excel_con.Close();
string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
sqlBulkCopy.DestinationTableName = "smarttable";
sqlBulkCopy.ColumnMappings.Add("InvoiceNumber", "InvoiceNumber");
sqlBulkCopy.ColumnMappings.Add("AmountPaid", "AmountPaid");
sqlBulkCopy.ColumnMappings.Add("Remarks", "Remarks");
con.Open();
sqlBulkCopy.WriteToServer(dtExcelData);
con.Close();
}
}
}
}
}
请帮忙。