如何在连接字符串中使用文件路径?

时间:2017-06-13 19:12:53

标签: c# asp.net

我目前正在创建一个Web应用程序来接收excel文件并将数据导入数据库。我当前的连接字符串如下

string connStringExcel = @"Provider=Microsoft.ACE.OLEDB.12.0;Data 
Source=C:\users\myname\downloads\example.xlsx;Extended 
Properties=""Excel 
12.0;HDR=YES;""";

我尝试插入文件上传控件,然后使用以下命令查找文件的路径:

string filePath = 
System.IO.Path.GetFullPath(fileUpload.PostedFile.FileName);

然后使用filePath变量替换连接字符串中的完整文件路径。我该怎么做才能选择自己的文件,以便不必硬编码?

Errors

3 个答案:

答案 0 :(得分:2)

string filePath = 
System.IO.Path.GetFullPath(fileUpload.PostedFile.FileName);
string connStringExcel = $"Provider=Microsoft.ACE.OLEDB.12.0;Data 
Source={filePath};Extended 
Properties='Excel 
12.0;HDR=YES;'";

答案 1 :(得分:1)

这对我很有帮助:

Private Sub Workbook_Open()    
    Dim oCN As WorkbookConnection
    
    'Loop through all the Workbook Connections
    For Each oCN In ThisWorkbook.Connections
        oCN.OLEDBConnection.Connection = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" + ThisWorkbook.Path + "\Database10.accdb;Mode=ReadWrite;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False;Jet OLEDB:Bypass UserInfo Validation=False;Jet OLEDB:Limited DB Caching=False;Jet OLEDB:Bypass ChoiceField Validation=False"
    Next 'Connections
    
    ThisWorkbook.RefreshAll
End Sub

在字符串 oCN.OLEDBConnection.Connection = ... 我采用原始连接字符串并用该构造替换路径 Data Source=" + ThisWorkbook.Path + "\Database10.accdb

答案 2 :(得分:0)

 <asp:FileUpload ID="fuManual" Enabled="false" runat="server"  />
  <asp:Button ID="btnUploadDoc" Text="Upload" runat="server" OnClick="UploadDocument"  />



     public void UploadDocument(object sender, EventArgs e)
        {
        try
        {

            if (fuManual.HasFile)
            {

                string manual_filename = ddlStore.SelectedItem.Text + "_" + "Manual.xlsx";

                string extension = Path.GetExtension(fuManual.PostedFile.FileName);

                // Import Excel Code for Manual Excel Data import....
                string FilePath = Server.MapPath("~/Temp/" + manual_filename);
                fuManual.SaveAs(FilePath);
                System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(
                            "Provider=Microsoft.ACE.OLEDB.12.0; " +
                             "data source='" + FilePath + "';" +
                                "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\" ");
                myConnection.Open();

                DataTable mySheets = myConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                DataSet ds_manualData = new DataSet();
                DataTable dt_manualData;

                for (int i = 0; i < mySheets.Rows.Count; i++)
                {
                    dt_manualData = makeDataTableFromSheetName(FilePath, mySheets.Rows[i]["TABLE_NAME"].ToString());
                    ds_manualData.Tables.Add(dt_manualData);
                }


                ViewState["ManualData"] = ds_manualData;
                DataTable dts = new DataTable();
                dts = ds_manualData.Tables[0];
                //here u have the whole excel file in the data table now send it to the database  
               //remember to send table u need SqlDbType-structured

               myConnection.Close();
            }
            else
            {
                lblError.Text = "please Select the file to be uploaded";
            }
        }
        catch (Exception ex)
        {
        }                                    
    }
    private static DataTable makeDataTableFromSheetName(string FilePath, string sheetName)
    {
        System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(
        "Provider=Microsoft.ACE.OLEDB.12.0; " +
        "data source='" + FilePath + "';" +
        "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\" ");

        DataTable dtImport = new DataTable();
        System.Data.OleDb.OleDbDataAdapter myImportCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + sheetName + "]", myConnection);
        myImportCommand.Fill(dtImport);
        return dtImport;

    }