Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 SQL Bulk Insert with asp.net

Author  Topic 

dougancil2009
Starting Member

17 Posts

Posted - 2010-05-04 : 12:56:19
I have almost finished a site that I've been working on but have one last thing that I don't know where to add. I have a bulk insert sql query that I want to insert into the .vb code behind and have no idea where to put it. Basically, the code is attached to a serverclick event that's to be triggered when a user clicks a submit button. Here is the .vb file as it is currently configured:

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
Try
File1.PostedFile.SaveAs(SaveLocation)
Response.Write(<center>Thank you for your submission.</center>)
Dim connection As String = ConfigurationManager.ConnectionStrings("Dialerresults").ConnectionString
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 sql bulk insert (please note that I won't need to create a new table as the table is already inserted. This sql query has been tested for the purposes of what I'm trying to do and works, so I need this query inserted into the .vb code behind)


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

Select *
from dialerresults
go


I'm assuming that this goes after the TRY statement but as I'm still learning asp.net, I have no idea how to call the command. Any assistance would be helpful.

Thank you

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-04 : 13:04:57
will the file name be always seem? i think it would be better for you to create a procedure for bulk insert with a parameter for passing file path and then calling it from click event.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

dougancil2009
Starting Member

17 Posts

Posted - 2010-05-04 : 13:09:08
Will the file name always be the same? or be seen? I don't want to create a stored procedure because that's one more point of failure for such a short command. I would much rather have it run from the codebehind. That's kind of how I would prefer to do it. In other words I trust my web server a lot more than I do my sql server to always do what it's supposed to do and it reduces my points of failure by 1.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-04 : 13:16:23
quote:
Originally posted by dougancil2009

Will the file name always be the same? or be seen? I don't want to create a stored procedure because that's one more point of failure for such a short command. I would much rather have it run from the codebehind. That's kind of how I would prefer to do it. In other words I trust my web server a lot more than I do my sql server to always do what it's supposed to do and it reduces my points of failure by 1.


i meant same
but if you're using a direct query will you be always sure of filename before hand?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

dougancil2009
Starting Member

17 Posts

Posted - 2010-05-04 : 14:44:34
I had wanted to write into the codebehind to do that but I had errors with that. I want to always call the file upload.txt and to verify that the aspx.vb is only uploading .txt files. If you could also describe that I would be grateful.

Thank you
Go to Top of Page

dougancil2009
Starting Member

17 Posts

Posted - 2010-05-04 : 14:56:56
Also,
Visakh, I understand that you want to write an xml to do a stored procedure but as I'm new to learning asp.net, I'd rather stick with this example right now (this project is a bit time sensitive and I can learn the xml later) so please understand that I know that this isn't the best way to write this code but for now, it will work for me.
Go to Top of Page
   

- Advertisement -