vendredi 3 mai 2019

Just imports if the excel file into Database if it is in IIS express folder

Well, I'm doing a program that imports data from an excel file into a database(sql server). The program runs fine if I save the excel file in IIS express folder but if I put in documents folder or something like it, it gives me an error:

here's the code:

protected void Upload_Click(object sender, EventArgs e)
    {
        string filepath = FileUpload1.PostedFile.FileName;
        string filename = Path.GetFileName(filepath);
        string ext = Path.GetExtension(filename);
        String strConnection = @"Data Source=PEDRO-PC\SQLEXPRESS;Initial Catalog=costumizado;Persist Security Info=True;User ID=sa;Password=1234";
        string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0 Xml;HRD=YES;IMEX=1;\"";

        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);

        OleDbCommand cmd = new OleDbCommand("Select [Nome],[Cidade],[Idade] from [Folha1$]", excelConnection);
        excelConnection.Open();

        //cmd.ExecuteNonQuery();


       // DataSet ds = new DataSet();
       // SqlDataAdapter da = new SqlDataAdapter("Select [Nome],[Cidade],[Idade] from [Folha1$]", strConnection);

        OleDbDataReader dReader;
        dReader = cmd.ExecuteReader();
        SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);

        sqlBulk.DestinationTableName = "Teste";
        sqlBulk.WriteToServer(dReader);
        excelConnection.Close();
    }




Aucun commentaire:

Enregistrer un commentaire