943,702 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 2229
  • VB.NET RSS
Dec 5th, 2008
0

Query and text file

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
leolim is offline Offline
6 posts
since Sep 2007
Dec 5th, 2008
0

Re: Query and text file

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
Reputation Points: 11
Solved Threads: 9
Light Poster
Rogachev is offline Offline
44 posts
since Nov 2008
Dec 6th, 2008
0

Re: Query and text file

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
leolim is offline Offline
6 posts
since Sep 2007
Dec 6th, 2008
0

Re: Query and text file

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



Click to Expand / Collapse  Quote originally posted by leolim ...
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
Reputation Points: 11
Solved Threads: 9
Light Poster
Rogachev is offline Offline
44 posts
since Nov 2008
Dec 7th, 2008
0

Re: Query and text file

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
leolim is offline Offline
6 posts
since Sep 2007
Dec 7th, 2008
0

Re: Query and text file

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.
Reputation Points: 11
Solved Threads: 9
Light Poster
Rogachev is offline Offline
44 posts
since Nov 2008
Dec 8th, 2008
0

Re: Query and text file

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
leolim is offline Offline
6 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: how to read this file
Next Thread in VB.NET Forum Timeline: Exporting to PDF in VS2005





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC