2013-09-04 2 views
1

Je suis en train de télécharger un fichier Excel à une base de données SQL et fonctionnait très bien sur mon ordinateur, mais une fois que je téléchargé sur le serveur, il me donne cette erreur:Le moteur de base de données Microsoft Jet n'a pas pu trouver l'objet

Exception Details: System.Data.OleDb.OleDbException: The Microsoft Jet database engine could not find the object 'C:\Windows\SysWOW64\inetsrv\Book1.xls'. Make sure the object exists and that you spell its name and the path name correctly.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Voici mon code:

protected void Button1_Click(object sender, EventArgs e) 
    { 
     string excelConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HRD=YES;IMEX=1'", FileUpload1.PostedFile.FileName); 
     using (OleDbConnection connection = new OleDbConnection(excelConnectionString)) 
     { 
      OleDbCommand command = new OleDbCommand(("Select * FROM [Sheet1$]"), connection); 
      connection.Open(); 
      using (DbDataReader dr = command.ExecuteReader()) 
      { 
       using (SqlBulkCopy bulkCopy = new SqlBulkCopy("Data Source=WSCJTCSQ1;Initial Catalog=TestDB;Persist Security Info=True;User ID=test;Password=test")) 
       {     
        bulkCopy.DestinationTableName = "CoaTest";       
        bulkCopy.ColumnMappings.Add("First Name", "fName"); 
        bulkCopy.ColumnMappings.Add("Last Name", "lName"); 
        bulkCopy.ColumnMappings.Add("Agency", "agency"); 
        bulkCopy.WriteToServer(dr); 
       } 
      } 
     } 
     Label1.ForeColor = System.Drawing.Color.Red; 
     Label1.Text = "Successfully Uploaded The New Roster"; 
    } 
+0

peut u afficher du code qui a sauvé le fichier? Dans quel dossier? –

+0

Lorsque je l'héberge sur ma machine locale, c'est juste sur le bureau. Dois-je d'abord l'enregistrer sur le serveur, puis le retirer à partir de là? – vadim

Répondre

2
string file = string.Format("{0}\\{1}", Request.PhysicalApplicationPath, 
Guid.NewGuid().ToString().Replace("-",string.Empty)); 

FileUpload1.SaveAs(file) 

maintenant dans votre code d'importation

string excelConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; 
Data Source={0};Extended Properties='Excel 8.0;HRD=YES;IMEX=1'", file); 
+0

Cela a fonctionné parfaitement! Merci beaucoup pour la réponse rapide !! – vadim

-1

vous devez enregistrer le fichier sur le serveur avant de s'y connecter, quelque chose comme:

string sServerFilespec = ("C:\\ServerFolder\\" + System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)); 
FileUpload1.PostedFile.FileName.SaveAs(sServerFilespec); 
+0

-ve parce que ce n'est pas la bonne approche pour accéder au chemin du serveur. essayez d'utiliser Server.MapPath() ou HttpRequest.PhysicalApplicationPath. –

Questions connexes