By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,204 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Urgent help Please

P: 19
Hi Guys

i write a thread before asking for help with reading an uploaded csv file,

i have my code, it reads the csv file and currently displays it in a datagrid but what i actually want is to take the read information and import it into my sql express db.

Heres the code

Expand|Select|Wrap|Line Numbers
  1. <script runat="server">    
  2.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
  3.         If (IsPostBack) Then
  4.             Grid1.Visible = True
  5.         Else
  6.             Grid1.Visible = False
  7.         End If
  8.     End Sub
  9.     Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  10.         'Save the uploaded file to an "Uploads" directory
  11.         ' that already exists in the file system of the 
  12.         ' currently executing ASP.NET application.  
  13.         ' Creating an "Uploads" directory isolates uploaded 
  14.         ' files in a separate directory. This helps prevent
  15.         ' users from overwriting existing application files by
  16.         ' uploading files with names like "Web.config".
  17.         Dim saveDir As String = "\Data\"
  18.  
  19.         ' Get the physical file system path for the currently
  20.         ' executing application.
  21.         Dim appPath As String = Request.PhysicalApplicationPath
  22.  
  23.         ' Before attempting to save the file, verify
  24.         ' that the FileUpload control contains a file.
  25.         If (FileUpload1.HasFile) Then
  26.             Dim savePath As String = appPath + saveDir + FileUpload1.FileName
  27.  
  28.             ' Call the SaveAs method to save the 
  29.             ' uploaded file to the specified path.
  30.             ' This example does not perform all
  31.             ' the necessary error checking.               
  32.             ' If a file with the same name
  33.             ' already exists in the specified path,  
  34.             ' the uploaded file overwrites it.
  35.             FileUpload1.SaveAs(savePath)
  36.  
  37.             ' Notify the user that the file was uploaded successfully.
  38.             UploadStatusLabel.Text = "Your file was uploaded successfully."
  39.  
  40.         Else
  41.             ' Notify the user that a file was not uploaded.
  42.             UploadStatusLabel.Text = "You did not specify a file to upload."
  43.         End If
  44.  
  45.     End Sub
  46.  
  47.     Sub DisplayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  48.         Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\inetpub\wwwroot\MerlinLocalPostOfficeApp\Data;Extended Properties=""text;HDR=NO;FMT=Delimited"""
  49.         Dim objConn As New OleDbConnection(sConnectionString)
  50.         objConn.Open()
  51.         Dim objCmdSelect As New OleDbCommand("SELECT * FROM csv.txt", objConn)
  52.         Dim objAdapter1 As New OleDbDataAdapter()
  53.         objAdapter1.SelectCommand = objCmdSelect
  54.          Dim objDataset1 As New DataSet()
  55.         objAdapter1.Fill(objDataset1, "csv.txt")
  56.         Grid1.DataSource = objDataset1.Tables(0).DefaultView
  57.         Grid1.DataBind()
  58.         objConn.Close()
  59.     End Sub
  60.  
  61.  
  62.   </script>
  63.  
My csv file does not have headers so the default value is F1, F2, F3 F4, F5,F6, F7, my database has 7 columns, ID,AddressLine1,AddressLine2,AddressLine3,AddressL ine4,AddressLine5,AddressLine6,Postcode

I need to know how to import the information direct into the db rather than displaying it on the page, ive tried but im really new to this and cant get it to work. I cant use DTS or bulk insert as the server this will go on doesnt have sql on it, the db is an MDF file so is transportable with the app.

Thanks for your help
Feb 25 '07 #1
Share this Question
Share on Google+
1 Reply


P: 9
First, I'm thinking that having an .mdf without an instance of SQL Server to attach to is fairly useless. Check the following usenet post for support Google Usenet Posting

So, thats rough and might need rethinking.

Second, to not display we just don't bind it to a control that renders to the page. Instead, we can read the .csv into a dataset, get a dataset of the table we pushing to and get on with life.

Store procedure would be good for this though. Cheers.
Feb 25 '07 #2

Post your reply

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