嗨,大家好,由于我最近几周什么都没做,只是问了些问题来启动我的第一个应用程序,我认为现在是时候分享财富并像我一样帮助新手了,因为你们都做了同样的事情,谢谢所有这些回答了我。
这段代码会将一个csv文件保存到一个目录中,然后您可以使用它将信息批量插入数据库中的特定表中,这是通过asp.net vb和sql完成的,为此,我使用了mdf。 我还让管理员能够在实际导入csv之前删除其中的信息。
<script runat="server">
Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'Save the uploaded file to an "Uploads" directory
' that already exists in the file system of the
' currently executing ASP.NET application.
Dim saveDir As String = "\Data\"
' Get the physical file system path for the currently
' executing application.
Dim appPath As String = Request.PhysicalApplicationPath
' Before attempting to save the file, verify
' that the FileUpload control contains a file.
If (FileUpload1.HasFile) Then
Dim savePath As String = appPath + saveDir + FileUpload1.FileName
' Call the SaveAs method to save the
' uploaded file to the specified path.
' Will overwrite existing file of same name
FileUpload1.SaveAs(savePath)
' Notify the user that the file was uploaded successfully.
UploadStatusLabel.Text = "Your file was uploaded successfully."
Else
' Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload."
End If
End Sub
Sub DeleteAllButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim SQLConn As New System.Data.SqlClient.SqlConnection
SQLConn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PostOffice.mdf;Integrated Security=True;User Instance=True"
Dim strInsert As String
Dim cmdInsert As SqlCommand
strInsert = "DELETE FROM LocalPostOffice WHERE(ID > 0)"
cmdInsert = New SqlCommand(strInsert, SQLConn)
SQLConn.Open()
cmdInsert.ExecuteNonQuery()
SQLConn.Close()
End Sub
Sub ImportButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim SQLConn As New System.Data.SqlClient.SqlConnection
SQLConn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PostOffice.mdf;Integrated Security=True;User Instance=True"
Dim strInsert As String
Dim cmdInsert As SqlCommand
strInsert = "BULK INSERT [LocalPostOffice] FROM [c:\Inetpub\wwwroot\MerlinLocalPostOfficeApp\Data\csv.txt] WITH (FIELDTERMINATOR = ',')"
cmdInsert = New SqlCommand(strInsert, SQLConn)
SQLConn.Open()
cmdInsert.ExecuteNonQuery()
SQLConn.close()
End Sub
</script>
希望这对以后的人有所帮助。
From: https://bytes.com/topic/net/insights/611447-csv-bulk-insert-delete