473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reading and posting a .csv file into a SQL database

347 100+
I have the following code:

Expand|Select|Wrap|Line Numbers
  1.     Private Function BuildDataTable(ByVal fileFullPath As String, ByVal seperator As Char) As DataTable
  2.         Dim myTable As New DataTable("MyTable")
  3.         Dim i As Integer = 0
  4.         Dim myRow As DataRow = Nothing
  5.         Dim fieldValues As String() = Nothing
  6.         'string FileToRead = Server.MapPath(fileFullPath);   
  7.         Dim myReader As System.IO.StreamReader = Nothing
  8.         Try
  9.             'Open file and read first line to determine how many fields there are.   
  10.             myReader = System.IO.File.OpenText(fileFullPath)
  11.             'string ReadContents = myReader.ReadToEnd();   
  12.             fieldValues = myReader.ReadLine().Split(seperator)
  13.             'Create data columns accordingly   
  14.             For i = 0 To fieldValues.Length - 1
  15.                 myTable.Columns.Add(New DataColumn(fieldValues(i)))
  16.             Next
  17.             'Adding the first line of data to data table   
  18.             myRow = myTable.NewRow()
  19.             For i = 0 To fieldValues.Length - 1
  20.                 myRow(i) = fieldValues(i).ToString()
  21.             Next
  22.             myTable.Rows.Add(myRow)
  23.             'Now reading the rest of the data to data table   
  24.             While myReader.Peek() <> -1
  25.                 fieldValues = myReader.ReadLine().Split(seperator)
  26.                 myRow = myTable.NewRow()
  27.                 For i = 0 To fieldValues.Length - 1
  28.                     myRow(i) = fieldValues(i).ToString()
  29.                 Next
  30.                 myTable.Rows.Add(myRow)
  31.             End While
  32.         Finally
  33.             myReader.Close()
  34.         End Try
  35.  
  36.         Return myTable
  37.     End Function
  38.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  39.         Dim dt As DataTable = BuildDataTable("\\path\Data\xxx.csv", ","c)
  40.         'Creating a new table for storing in database 
  41.         'Extracting phone number, patient name, doctors name, appointment date and appointment time'  
  42.         Dim dtForDB As New DataTable()
  43.         dtForDB.Columns.Add("PhoneNumber")
  44.         dtForDB.Columns.Add("PatientName")
  45.         dtForDB.Columns.Add("DrName")
  46.         dtforDB.Columns.Add("apptdate")
  47.         dtforDB.Columns.Add("apptime")
  48.         For Each dr As DataRow In dt.Rows
  49.             dtForDB.Rows.Add(dr("Phonenumber").ToString(), dr("PatientName").ToString(), dr("DrName").ToString(), dr("apptdate").ToString(), dr("appttime").ToString())
  50.         Next
  51.     End Sub
  52.  
The only information that I'm needing from this data starts after the bold section and then from that (as you can see from my code) all I will need is

Phone Number (###) ###-####
Patients Name Doe, John
Doctors Name xxxxxxxx
Appointment Time 7:30 AM
Appointment date 7/12/2010

This file is being uploaded by a client through a web page that will immediately post it to a sql server. The file comes to me as a .csv file (though as you can see, it's really not .csv) and I'm wondering the best methodology for sorting through it and getting only the information that I need. Would it be better to sort the data or do a select? Also, since I'm new to this methodology, could someone please include a sample of the code that they would use to achieve something like this?

And Christopher if you look at this and respond, please know that this code has been tested and it does work, with the sample data from above. It is achievable, just a bit of work and as you can see, still a work in progress.

Thank you,

Doug
Jul 21 '10 #1
1 1274
Frinavale
9,735 Expert Mod 8TB
What control are you using to display the table?
In that control you can specify which columns should be displayed and which shouldn't.

-Frinny
Jul 23 '10 #2

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

Similar topics

6
by: matt | last post by:
I am using a text file as a database, each field delimited by || I want to be able to print all lines to a page, then by selecting one (with, say, a radio button) it will load into the form at the...
2
by: mitesh.n.gandhi | last post by:
Hi, I would like to Write XML document by reading .xsd file using vb.net/c#.net. May be this Question has been answered earlier. Please if anyone knows the link or example let me know. ...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
21
by: EdUarDo | last post by:
Hi all, I'm not a newbie with C, but I don't use it since more than 5 years... I'm trying to read a text file which has doubles in it: 1.0 1.1 1.2 1.3 1.4 2.0 2.1 2.2 2.3 2.4 I'm doing...
12
by: mitek777 | last post by:
Hi, I have a problem with reading from file. I would like to find some string(f e.g. name) in file, and if it exist show it on screen with second and third line under. I have this in file: ...
1
by: John | last post by:
I have a process that reads a text file then uploads the data into a database table. The text file has 10 lines at the end of the file that are blank BUT it appears that the enter key or space bar...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
8
by: Lonifasiko | last post by:
Hi, Using Process class I asynchronously launch an executable (black box executable) file from my Windows application. I mean asynchronously because I've got an EventHandler for "Exited" event....
4
by: archana | last post by:
Hi all, I want to read csv file into datatable. Is there any csv reader and writer available for freeware. I am reading csv file using schema.ini file. I don't want this dependency. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.