隅に追いやられて……。
ウェブで見つけたコードを使用しましたが、この接続を閉じる方法がわかりません。返された 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 回の試行が行われました。
これについては本当に助けが必要です。