Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Add Value From Text File To Database Using Vb.net And Ms Sql

VB.NET textfile database VB.net MS SQL value

  • This topic is locked This topic is locked
2 replies to this topic

#1 poojab

poojab

    CC Lurker

  • New Member
  • Pip
  • 2 posts
  • Programming Language:Java, (Visual) Basic, PL/SQL, Visual Basic .NET, Transact-SQL
  • Learning:Java, (Visual) Basic, PL/SQL, Visual Basic .NET, Transact-SQL

Posted 23 July 2012 - 10:42 PM

Hello Friends,

I want to know how can I add values from different text files to database using VB.net code....

To be more clear...
In my application.StartupPath I have a folder named PatientDetails
In this folder other folders will get created according to Patient id's
In this folder there will be the text files containing the patient complete details....

Now what I want is that the database shud get all the values from all the folders and save the values in the MS SQL database...

Can anyone help me on this???

Regards,
Pooja
  • 0

#2 poojab

poojab

    CC Lurker

  • New Member
  • Pip
  • 2 posts
  • Programming Language:Java, (Visual) Basic, PL/SQL, Visual Basic .NET, Transact-SQL
  • Learning:Java, (Visual) Basic, PL/SQL, Visual Basic .NET, Transact-SQL

Posted 24 July 2012 - 12:34 AM

I got the code working....

Find below

'open database connection
Open_DB_Connection()
Dim i As Integer = 1
Dim dir As DirectoryInfo = New DirectoryInfo(Application.StartupPath & "\Images\")

'get the number of folders in the main folder
Dim number As Integer
number = System.IO.Directory.GetDirectories(dir.ToString).Length
Debug.Print("number: " + number.ToString)
Dim myCommand As SqlCommand

'delete the data if any present in database
Try
Dim myCommandD As SqlCommand
myCommandD = New SqlCommand("DELETE FROM Test.dbo.Test3project", Connection)
myCommandD.ExecuteNonQuery()

'check if main folder is empty
If number = 0 Then
MsgBox("No files found")
Else
'get the array of directories
Dim mainfolder As DirectoryInfo() = dir.GetDirectories()
Dim foldername As DirectoryInfo

For Each foldername In mainfolder
Debug.Print("folder name: " + foldername.ToString)
Dim sr As StreamReader = New StreamReader(Application.StartupPath + "\Images\" + foldername.ToString + "\patientreg.txt")
'read the content of the file
Dim line As String = sr.ReadLine()

'split the file according to the separator
Dim fields() As String = line.Split("*")

'insert the data into database
myCommand = New SqlCommand("INSERT INTO Test.dbo.Test3project VALUES('" & fields(0) & "','" & fields(1) & "', '" & fields(2) & "', '" & fields(3) & "', '" & fields(4) & "','" & fields(5) & "','" & fields(6) & "')", Connection)
Debug.Print("Query: " + myCommand.CommandText)
myCommand.ExecuteNonQuery()

Next
MsgBox("Saved...showing data to datagrid")

'showing database data to datagrid
Try
Dim myCommand1 As New SqlCommand
With myCommand1
.CommandText = "select * from Test.dbo.Test3project"
.CommandType = CommandType.Text
.Connection = Connection
End With
Dim dt As New DataTable
dt.Load(myCommand1.ExecuteReader)
With DataGridView1
.AutoGenerateColumns = True
.DataSource = dt
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Close_DB_Connection()

  • 0

#3 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 24 July 2012 - 03:50 AM

This topic has been marked as SOLVED. If you have a similar question or topic, go back to the subforum and start a new topic to continue discussions.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download