Solved

VB.NET export results of sql query to textfile

Posted on 2008-06-12
4
1,258 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
  • 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 50 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now