ASP.NET SQL Bulk Insert

Joined
Apr 16, 2009
Messages
4
Reaction score
0
I have a simple upload page that will upload a file to a server and when the "upload" button is clicked, the file is uploaded to the server and then a bulk insert command is made from the .vb file. I have the database connection in the web.config file but I don't know where to add the ado connection and command structure in the .vb file. I also don't know where to put the bulk insert. I'm assuming that it goes in the .vb file as well.

Here is my .vb file as it stands currently


Partial Class _Default
Inherits System.Web.UI.Page


Private Sub Submit1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick

If Not File1.PostedFile Is Nothing And File1.PostedFile.ContentLength > 0 Then
Dim fn As String = System.IO.Path.GetFileName(File1.PostedFile.FileName)
Dim SaveLocation As String = Server.MapPath("Data") & "\" & fn
Dim fileName As String = Server.HtmlEncode(File1.FileName)
' Get the extension of the uploaded file.
Dim extension As String = System.IO.Path.GetExtension(fileName)
' Allow only files with .txt extensions' to be uploaded.
Try
File1.PostedFile.SaveAs(SaveLocation)
Response.Write(<center>Thank you for your submission.</center>)
Catch Exc As Exception
Response.Write("Error: " & Exc.Message)


End Try
Else
Response.Write(<center>Please select a file to upload.</center>)
End If

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
End Class


Here is the bulk insert command that I need to be triggered by the ServerClick event:

bulk insert dialerresults
from '\\MSBWEB3\data\upload.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

Select *
from dialerresults
go

The table is already created, I don't want to do a stored procedure to do this, I just need to call the connection string from the aspx.vb page and to add the bulk insert to the sub after verifying that the file in in fact uploaded and that it is in the correct format.

Can anyone assist me on how to do this?

Thank you,

Doug
 
Back
Top