Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
How to make code run differently depending on the platform it is running on?
xarzu - Apr 05 2019 09:17 AM
How do I set a breakpoint in an attached process in visual studio
xarzu - Apr 04 2019 11:47 AM
Recent Blog Entries
Recent Status Updates
- Managed C++
- Visual Basic 4 / 5 / 6
- linked list
- hello world
[SOLVED] Add Value From Text File To Database Using Vb.net And Ms SqlVB.NET textfile database VB.net MS SQL value
Posted 23 July 2012 - 10:42 PM
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???
Posted 24 July 2012 - 12:34 AM
'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()
Posted 24 July 2012 - 03:50 AM
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.
Also tagged with one or more of these keywords: VB.NET, textfile, database, VB.net, MS SQL, value
Language Forums →
Language Forums →
Tutorial Forums →
Visual Basic Tutorials →
General Forums →
General Programming →
General Forums →
General Computing →