Hola que tal, quiero guardar el resultado de un query en un txt pero en vez de los valores arrojados me guarda el query como tal, les dejo el codigo esperando me puedan ayudar con mi problema, o si tienen otra forma de guardar el resultado de un query en un txt estoy abierto a opciones, de antemano gracias a todos, saludos, por cierto estoy usando webforms en vb.net pero aunque tengan soluciones para C# son bienvenidas, saludos.
Const FILE_NAME As String = "MyFile.txt"
If File.Exists(FILE_NAME) Then
Console.WriteLine("{0} already exists.", FILE_NAME)
Return
End If
Using sw As StreamWriter = File.CreateText(FILE_NAME)
Dim query As String = "SELECT F4211A.SDAN8, F4211A.SDLITM, (F4211A.SDDSC1+'-'+F4211A.SDDSC2), F4211A.SDSOQS, F4211A.SDAPUM, (F4211A.SDLPRC/10000), cast((F4074A.ALFVTR/10000) as varchar), cast( (SELECT (F4074AB.ALFVTR/10000) FROM " + BDowner1 + ".F4074 F4074AB WHERE F4074AB.ALDOCO=F4211A.SDDOCO AND F4074AB.ALDCTO=F4211A.SDDCTO AND F4074AB.ALKCOO=F4211A.SDKCOO AND F4074AB.ALLNID =F4211A.SDLNID AND F4074AB.ALOSEQ>=20 AND F4074AB.ALOSEQ<=50) as varchar), (F4211A.SDUPRC/10000), (F4211A.SDAEXP/100), (SELECT F58STATAB.STDSC1 FROM " + BDowner1 + ".F58STAT F58STATAB WHERE F4211A.SDLTTR=F58STATAB.STLTTR AND F4211A.SDNXTR=F58STATAB.STNXTR)FROM " + BDowner1 + ".F4211 F4211A INNER JOIN " + BDowner1 + ".F4074 F4074A ON F4074A.ALDOCO=F4211A.SDDOCO AND F4074A.ALDCTO=F4211A.SDDCTO AND F4074A.ALKCOO=F4211A.SDKCOO AND F4074A.ALLNID =F4211A.SDLNID WHERE F4211A.SDKCOO = " & Compania & " AND F4211A.SDMCU='" & Almacen & "' AND F4074A.ALOSEQ>0 AND F4074A.ALOSEQ<20 AND F4211A.SDDOCO='" & TxtPedFact.Text.Trim & "'"
'son 8 campos
Dim campos As Integer
Dim resultado As String = ""
Dim conn As New SqlConnection
Dim comando As New SqlCommand(query, conn)
Dim dreader As SqlDataReader
Try
If (conn.State = ConnectionState.Closed) Then
conn.Open()
End If
comando.ExecuteReader()
If (dreader.Read()) Then
For campos = 0 To 7
resultado += dreader.GetValue(campos) + "\t"
Next
End If
Catch ex As Exception
Finally
conn.Close()
End Try
' sw.WriteLine("This is my file.")
' sw.WriteLine("I can write ints {0} or floats {1}, and so on.", 1, 4.2)
sw.WriteLine(query)
sw.Close()
End Using