473,756 Members | 4,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CSV Bulk insert and Delete

19 New Member
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 8971

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

Similar topics

2
7056
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 this process will move much faster. Can someone suggest me a way to do it? Thanks declare
2
4966
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 in the database 3-Imports data using bulk insert 4-Analyzes data using stored procedures I would like to improve the performance of the analysis in step 4 by
2
16507
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. When I execute the query, I get an error saying that only sysadmin or bulkadmin roles are allowed to use the BULK INSERT statement. So, I proceeded with the Enterprise Manager to grant myself those roles. However, I could not find sysadmin or...
7
12129
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 servers using sp_addlinkedserver on both database servers. When I call the Commit API of oledb I get the following error: Error state: 1, Severity: 19, Server: TST-PROC22, Line#: 1, msg:
6
12357
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 10 columns into a table with 12. How can I specify which columns to insert to? I think format files are what I'm supposed to use, but I can't figure them out. I've also tried using a view, as was suggested on one of the many websites I've...
16
17017
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 must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then IF FOUND UPDATE <TABLE> SET .... <Values entered here> ELSE INSERT INTO <TABLE> VALUES <Values...
2
8605
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 that temp table -drop constraints on original table -drop the original table -rename the temporary table
0
3014
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 accounting ctr is the last three letters of the text file being used for the BULK INSERT. How would you suggest that I do this? Is there a way to add a default value to the .fmt or schema files for the text fields, or in the BULK INSERT...
4
6578
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 are done in batches of 50K. I am facing a very peculiar performance issue with regard to these bulk operations. The initial batch of bulk insert (50K records) is finished within 3 seconds; while the next similar batch for bulk insert or update...
0
9255
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9844
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9819
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8688
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.