Я только что создал приложение mvc 4, в этом приложении у меня есть функция для загрузки файла Excel в базу данных.«Свойство ConnectionString не было инициализировано». Ошибка в SQL bulkcopy upload
Это нормально работает в локальном хосте.
Но когда я раскрываю в IIS и попытаться загрузить Excel file.I'm получать следующие ошибки
Свойство ConnectionString не был инициализирован.
Эти строки соединения, я использую для загрузки файлов Excel
<add name="dbconnection" connectionString="Data Source="000.000.00.00";Initial Catalog=AFFHEC_DB;Persist Security Info=True;User ID=**;Password=****" providerName="System.Data.SqlClient" />
<add name="constr" connectionString="data source=000.000.0.000;Initial catalog=AFFHEC_DB;user id=**;password=*****;" />
<add name="Excel07+ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES';" />
<add name="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'" />
Это код для SQL массового копирования загрузить
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Student(HttpPostedFileBase FileUpload1, tbl_hec_Programme programme)
{
try
{
string conString = string.Empty;
//Upload and save the file
if (Request.Files["FileUpload1"].ContentLength > 1)
{
try
{
string excelPath = Path.Combine(HttpContext.Server.MapPath("~/Content/"), Path.GetFileName(FileUpload1.FileName));
FileUpload1.SaveAs(excelPath);
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
conString = string.Format(conString, excelPath);
}
catch (Exception ex)
{
this.SetNotification("The file type submitted is invalid", NotificationEnumeration.Error);
}
try
{
using (OleDbConnection excel_con = new OleDbConnection(conString))
{
excel_con.Open();
DataTable dtExcelData = new DataTable();
string query = "SELECT " +
"s1.HEC_ID, " +
"s1.Student_Personal_ID, " +
"s1.Student_Passport_Number, " +
"s1.Student_ID_by_University, " +
"s1.First_Name, " +
// // join three tables
"FROM (((([Personal_Data$] as s1) " +
"LEFT OUTER JOIN [Enrolment_Data$] as s2 ON s1.HEC_ID = s2.HEC_ID) " +
using (OleDbDataAdapter oda = new OleDbDataAdapter(query, excel_con))
{
oda.Fill(dtExcelData);
}
string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con,
SqlBulkCopyOptions.CheckConstraints |
SqlBulkCopyOptions.FireTriggers |
SqlBulkCopyOptions.KeepNulls |
SqlBulkCopyOptions.TableLock |
SqlBulkCopyOptions.UseInternalTransaction |
SqlBulkCopyOptions.KeepIdentity,
null))
{
//Set the database table name
sqlBulkCopy.DestinationTableName = "tbl_HEI_student";
sqlBulkCopy.BulkCopyTimeout = 0;
sqlBulkCopy.ColumnMappings.Add("ID", "ID");
sqlBulkCopy.ColumnMappings.Add("Student_Personal_ID", "Student_Personal_ID_CPR");
sqlBulkCopy.ColumnMappings.Add("Student_Passport_Number", "Student_Passport_Number");
................
con.Open();
try
{
sqlBulkCopy.WriteToServer(dtExcelData);
int totalRowsAdded = dtExcelData.Rows.Count;
UploadStatusWriter(User.Identity.GetUserId(), Status_type, Date_type);
this.SetNotification("Student data successfully uploaded", NotificationEnumeration.Success);
return RedirectToAction("StudentIndex", "HEI");
}
catch (Exception ex)
{
this.SetNotification(ex.Message, NotificationEnumeration.Error);
return RedirectToAction("Student", "Excel");
}
finally
{
con.Close();
}
}
}
}
}
catch (Exception ex)
{
this.SetNotification(ex.Message, NotificationEnumeration.Error);
return RedirectToAction("Student", "Excel");
}
}
else
{
this.SetNotification("Please select a file first and then click the submit button", NotificationEnumeration.Error);
return RedirectToAction("Student", "Excel");
}
}
catch (Exception ex)
{
return RedirectToAction("Student", "Excel");
}
}
Не могли бы вы показать свой код? –
Как вы запрашиваете db? [это может помочь вам] (http://stackoverflow.com/questions/1007786/how-to-fix-the-connectionstring-property-has-not-been-initialized) – Tikkes
Нам нужно увидеть код, в котором вы взаимодействуете с Db и используя соединение. –