?
Solved

VB.NET export results of sql query to textfile

Posted on 2008-06-12
4
Medium Priority
?
1,289 Views
Last Modified: 2008-06-12
Hey all,
I've a question about VB.NET and SQL Querys.
My problem of the code below is, how do I write the results of the Query into a textfile?!
The SQL connection ect...works fine....but I don't know how can i write it to a textfile (.txt)

Private Sub SqlCon()
        Dim fso = CreateObject("Scripting.FileSystemObject")
        Dim MyFile = fso.CreateTextFile("\\FILESERVER\Logs\" & Date.Today & "-" & TimeOfDay.Hour & "-" & TimeOfDay.Minute & ".txt", True)
        Dim connString As String = "CONNECTION STRING HERE"
        Dim conn As New SqlConnection(connString)
        conn.Open()
        Dim cmdString As String = "SELECT * FROM BLABLA"
        Dim cmd As New SqlCommand(cmdString, conn)
        Dim reader As SqlDataReader = cmd.ExecuteReader()

        '????
        MyFile.Writeline(reader.Item("COLUMN HERE")) 'wie mach ich das?
        '????
    End Sub

SQL
0
Comment
Question by:sql_insider
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Expert Comment

by:molku
ID: 21770418
You must loop through the reader and call Read(). See code (not quite sure of the VB syntax)
While reader.Read()
   MyFile.Writeline(reader.Item("COLUMN HERE")) 
End While

Open in new window

0
 

Author Comment

by:sql_insider
ID: 21770562
Hey molku....
my code:

        Dim reader As SqlDataReader = cmd.ExecuteReader()
        While reader.Read()
            MyFile.WriteLine(reader.Item("ruleid"))
        End While

But it doesn't work...I will get an error:
"Unhandeld exception has occurred in....
Type mismathc. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"

SQ
0
 
LVL 4

Accepted Solution

by:
molku earned 200 total points
ID: 21770597
reader.Item returns an Object. What happens if you do this?
MyFile.WriteLine(reader.Item("ruleid").ToString())

Open in new window

0
 

Author Comment

by:sql_insider
ID: 21770637
Hey molku.....
thank you very much.....it works fine.......great.........

Thanks again

SQ
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question