i am newbie to this

could anyone show me how to retrieve the data from sql server and export the data to txt file? in vb.net windows application??

i want the same row and column of data retrieve from sql server to be insert and stored in the text file (.txt)

thanks

Recommended Answers

All 6 Replies

Hope this code can help you.

Dim _streamWriter As System.IO.StreamWriter
Dim _fileName As String = "fileName.txt"


If _table IsNot Nothing AndAlso _table.Rows.Count > 0 Then
_streamWriter = New System.IO.StreamWriter(_fileName)
For Each _row As DataSet1.EmployeesRow In _table.Rows
Dim _string As String = ""
For Each _column As DataColumn In _table.Columns
_string += _row.Item(_column.ColumnName).ToString + Chr(Keys.Tab)
Next
_streamWriter.WriteLine(_string)
Next
_streamWriter.Close()
End If

could you give a more specific example on how to retrieve from the table example employee table

name id add dob
JOHN 23 street 1 23/07/1980
TOMY 20 street 4 11/03/1979

and in the text file i want the record to be exectly the same
JOHN 23 street1 23/07/1980
TOMY 20 street 4 11/03/1979


how should i do this?

thanks

Hello.

I did not understand Have you got table (your employee datatable)?
You must to take data to dataset from database.
For example you have got table in the dataset. The name of this table is "Employee".
I am sure next code will give you result.


dim _streamWriter as StreamWriter
dim _table as DataTable = _dataSet.Tables("Employee")

'Check table. If table has is not nothing and has any records you will write rows to text file.
if _table isnot nothing andalso _table.Rows.Count > 0 then
_streamWriter = New System.IO.StreamWriter(myFileName)
for each _row as DataRow in _table.Rows
Dim _name as String = _row.Item("Name")
Dim _id as String = _row.Item("ID").ToString()
Dim _add as string = _row.Item("Add")
Dim _dob as string = Format(_row.Item("dob"), "dd/MM/yyyy")

_streamWriter.WriteLine(_Name & " " & _id & " " & _add & " " & _dob)

Next
_streamWriter.Close()
end if


could you give a more specific example on how to retrieve from the table example employee table

name id add dob
JOHN 23 street 1 23/07/1980
TOMY 20 street 4 11/03/1979

and in the text file i want the record to be exectly the same
JOHN 23 street1 23/07/1980
TOMY 20 street 4 11/03/1979


how should i do this?

thanks

thanks for the advise.
i understand that the data inserted in the text file is seperated by " " right?
how about if i want to insert the data from the text file into another table? how do i ensure that the data inserted correctly into another table according to the to the seperation of the " " in the text file?

JOHN 23 street 1 23/07/1980

if i get the seperation by " " the street 1 suppose to be one value will become 2 value as street will be seperated by " " then 1 in the txt file. how should i do this?

Of cause the using " " as separator is really bad idea.
You need to use CVS or XML for your task...
I have seen great item in this forum about working with XML for very similar task. Try it - it's good idea.

can i seperate the data from sql usng tab where using /t instead of " "?? and can you show me how to insert from text file into a table called employee_backup with the same data field?


thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.