[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

VB.NET export results of sql query to textfile

Posted on 2008-06-12
4
Medium Priority
?
1,300 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

Industry Leaders: 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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 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