Solved

VB.NET export results of sql query to textfile

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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