CSV批量插入和删除

嗨,大家好,由于我最近几周什么都没做,只是问了些问题来启动我的第一个应用程序,我认为现在是时候分享财富并像我一样帮助新手了,因为你们都做了同样的事情,谢谢所有这些回答了我。

这段代码会将一个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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值