vendredi 3 mai 2019

cannot open or write to the file It is already opened exclusively by another user, or you need permission to view and write its data.'

Cannot save my excel file in a folder that I create in my project... I think I forgot something while doing the especification of the path... Maybe I have to do something after? I Already put the users with permissions and things like that, I THINK that the file isnt open I just have 1 user and nothing is open.. here's the code guys:

protected void Upload_Click(object sender, EventArgs e)
    {
        string excelPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.SaveAs(excelPath);


        string filepath = Server.MapPath("~/Files/");
        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 [Name],[city],[age] from [Sheet1$]", excelConnection);


            excelConnection.Open();




            cmd.ExecuteNonQuery();


            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("Select [Name],[city],[age] from [Sheet1$]", strConnection);

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

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


    }




Aucun commentaire:

Enregistrer un commentaire