把自己撑在一个角落里......
使用了我在网上找到的一段代码,但无法弄清楚如何关闭此连接。返回的 OleDbcommand objCommand 在处理后保持打开状态。我需要将其关闭,以便在从中提取数据后删除该文件。(不希望他们在服务器上闲逛。)
这必须比我尝试执行的 100 行代码更容易。该函数打开连接。
Protected Function ExcelConnection() As OleDbCommand
Dim fileName As String = Session("newUploadedFile")
' Connect to the Excel Spreadsheet
Dim xConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Server.MapPath(String.Format("~/Upload/{0}", fileName)) & ";" & _
"Extended Properties=Excel 8.0;"
' create your excel connection object using the connection string
Dim objXConn As New OleDbConnection(xConnStr)
objXConn.Open()
' use a SQL Select command to retrieve the data from the Excel Spreadsheet
' the "table name" is the name of the worksheet within the spreadsheet
' in this case, the worksheet name is "Sheet1" and is expressed as: [Sheet1$]
Dim objCommand As New OleDbCommand("SELECT Name FROM [Sheet1$]", objXConn)
Return objCommand
End Function
我试过了...
ExcelConnection.connection.close()
以及大约 40 次其他尝试重新创建命令然后关闭它。
真的可以在这方面使用一些帮助。