Vb.net 如何上传.csv文件?
这是我的代码,我得到一个错误,说: D:\VPN\u TFS\u ETP\ETP\u DEV\EndoAdmin\Uploads\Employers\u C\u d5pjixjyhcwojaypfyw0paoj\u Master “客户端报告可重新加载.csv”不是有效路径。确保 路径名拼写正确,并且您已连接到 文件所在的服务器 我想上传的文件扩展名是.csvVb.net 如何上传.csv文件?,vb.net,Vb.net,这是我的代码,我得到一个错误,说: D:\VPN\u TFS\u ETP\ETP\u DEV\EndoAdmin\Uploads\Employers\u C\u d5pjixjyhcwojaypfyw0paoj\u Master “客户端报告可重新加载.csv”不是有效路径。确保 路径名拼写正确,并且您已连接到 文件所在的服务器 我想上传的文件扩展名是.csv Dim oOLEDBConn As New OleDb.OleDbConnection Dim oDA As New OleDb.Ole
Dim oOLEDBConn As New OleDb.OleDbConnection
Dim oDA As New OleDb.OleDbDataAdapter
Dim oFile As HttpPostedFile, strFileName As String, strOrigName As String
Try
strOrigName = System.IO.Path.GetFileName(File1.Value)
oFile = File1.PostedFile
'
' Keep Copy in the Server
'
strFileName = Request.ServerVariables("APPL_PHYSICAL_PATH") & "Uploads\Employers_" & oUser.Usr_UserID & "C_" & Session.SessionID & "_" & strOrigName
oFile.SaveAs(strFileName)
'strFileName = Request.ServerVariables("APPL_PHYSICAL_PATH") & "Uploads\Co_" & Session.SessionID & "_" & strOrigName
'oFile.SaveAs(strFileName)
Dim OConnectionString As String, myStrmReader As New System.IO.StreamReader(strFileName)
'OConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strFileName + ";Extended Properties=Excel 8.0;"
Try
'OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly + ";Extended Properties=\"Text;HDR=" + header + "\""
OConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strFileName + ";Extended Properties='Text;HDR=Yes;FMT=Delimited;'"
'OConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strFileName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"
'OConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " + strFileName + "; Extended Properties=HTML Import;"
oOLEDBConn.ConnectionString = OConnectionString
oOLEDBConn.Open()
Catch ex1 As Exception
If ex1.Message.StartsWith("External table is not in the expected format") Then
'Try
' oOLEDBConn.Close()
' ' Load as HTML
' OConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strFileName + ";Extended Properties='HTML Import;HDR=Yes;IMEX=1;'"
' oOLEDBConn.ConnectionString = OConnectionString
' oOLEDBConn.Open()
'Catch ex2 As Exception
' lblFeedBack.Text = "22:" & ex2.Message
' Exit Sub
'End Try
lblFeedBack.Text = ex1.Message '& " Please see below."
Exit Sub
Else
lblFeedBack.Text = ex1.Message
Exit Sub
End If
End Try
使用连接字符串访问csv文件时,您不会指向要打开的文件名,而是指向文件所在的文件夹。稍后在查询中指定文件名。 因此,您的代码可以如下所示:
Dim folderWhereIsYourCsv = Request.ServerVariables("APPL_PHYSICAL_PATH") & "Uploads\"
OConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + folderWhereIsYourCsv + ";Extended Properties='Text;HDR=Yes;FMT=Delimited;'"
oOLEDBConn.ConnectionString = OConnectionString
oOLEDBConn.Open()
以后,当您要运行文件内容时。你应该这样做:
Dim fileNameOfYourCsv = "Employers_" & oUser.Usr_UserID & "C_" & Session.SessionID & "_" & strOrigName
Dim cmd = new OleDbCommand("SELECT * FROM " & fileNameOfYourCsv, oOLEDBConn);