473,396 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

CSV Bulk insert and Delete

Hi Guys, since I've done nothing but ask questions these last few weeks to get my first application up and running I thought it was about time to share the wealth and help out a newbie like me since you all did the same, thanks for all that replied to me.

This code will save a csv file to a dir, you can then use this to bulk insert the information to a specific table in your DB, this is done with asp.net vb and sql, I'm using an mdf for this. I also gave admins the ability to delete the info in there before they actually import the csv.


Expand|Select|Wrap|Line Numbers
  1. <script runat="server">    
  2.     Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  3.         'Save the uploaded file to an "Uploads" directory
  4.         ' that already exists in the file system of the 
  5.         ' currently executing ASP.NET application.
  6.         Dim saveDir As String = "\Data\"
  7.  
  8.         ' Get the physical file system path for the currently
  9.         ' executing application.
  10.         Dim appPath As String = Request.PhysicalApplicationPath
  11.  
  12.         ' Before attempting to save the file, verify
  13.         ' that the FileUpload control contains a file.
  14.         If (FileUpload1.HasFile) Then
  15.             Dim savePath As String = appPath + saveDir + FileUpload1.FileName
  16.  
  17.             ' Call the SaveAs method to save the 
  18.             ' uploaded file to the specified path.
  19.             ' Will overwrite existing file of same name
  20.             FileUpload1.SaveAs(savePath)
  21.  
  22.             ' Notify the user that the file was uploaded successfully.
  23.             UploadStatusLabel.Text = "Your file was uploaded successfully."
  24.  
  25.         Else
  26.             ' Notify the user that a file was not uploaded.
  27.             UploadStatusLabel.Text = "You did not specify a file to upload."
  28.         End If
  29.  
  30.     End Sub
  31.  
  32.     Sub DeleteAllButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  33.         Dim SQLConn As New System.Data.SqlClient.SqlConnection
  34.         SQLConn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PostOffice.mdf;Integrated Security=True;User Instance=True"
  35.         Dim strInsert As String
  36.         Dim cmdInsert As SqlCommand
  37.         strInsert = "DELETE FROM LocalPostOffice WHERE(ID > 0)"
  38.         cmdInsert = New SqlCommand(strInsert, SQLConn)
  39.         SQLConn.Open()
  40.         cmdInsert.ExecuteNonQuery()
  41.         SQLConn.Close()
  42.     End Sub
  43.  
  44.     Sub ImportButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  45.         Dim SQLConn As New System.Data.SqlClient.SqlConnection
  46.         SQLConn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PostOffice.mdf;Integrated Security=True;User Instance=True"
  47.         Dim strInsert As String
  48.         Dim cmdInsert As SqlCommand
  49.         strInsert = "BULK INSERT [LocalPostOffice] FROM [c:\Inetpub\wwwroot\MerlinLocalPostOfficeApp\Data\csv.txt] WITH (FIELDTERMINATOR = ',')"
  50.         cmdInsert = New SqlCommand(strInsert, SQLConn)
  51.         SQLConn.Open()
  52.         cmdInsert.ExecuteNonQuery()
  53.         SQLConn.close()
  54.     End Sub
  55.  
  56.   </script>
Hope this helps someone in the future.
Mar 4 '07 #1
0 8945

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Diego | last post by:
HI, I'm trying yo improve the performance of the following piece of code. Here I'm archiving Items that are done processing to Archive Tables. I believe that if I use BULK INSERTS/SELECTS/UPDATES...
2
by: Chris | last post by:
Any help would be appreciated. I am running a script that does the following in succession. 1-Drop existing database and create new database 2-Defines tables, stored procedures and functions...
2
by: php newbie | last post by:
Hello, I am trying to load a simple tab-delimited data file to SQL Server. I created a format file to go with it, since the data file differs from the destination table in number of columns. ...
7
by: iqbal | last post by:
Hi all, We have an application through which we are bulk inserting rows into a view. The definition of the view is such that it selects columns from a table on a remote server. I have added the...
6
by: pk | last post by:
Sorry for the piece-by-piece nature of this post, I moved it from a dormant group to this one and it was 3 separate posts in the other group. Anyway... I'm trying to bulk insert a text file of...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
2
by: Zarrin | last post by:
Hello, I read several articles of newsgroup about the bulk delete, and I found one way is to: -create a temporary table with all constraints of original table -insert rows to be retained into...
0
by: ozkhillscovington | last post by:
We have sp's in place that do BULK INSERTS from txt files into the tables. This works fine, however they have asked us to add a field that identifies accounting ctr. The only thing that identifies...
4
by: shreyask | last post by:
I have been working on doing bulk updates and inserts through sqlapi++ using a bulk udpate extention library, developed, a long time ago, internally. The database is oracle. The bulk inserts/updates...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.