Visual Basic.NET - primera línea en un txt (SqlDataReader)

 
Vista:
sin imagen de perfil

primera línea en un txt (SqlDataReader)

Publicado por Oscar (2 intervenciones) el 17/12/2013 19:29:43
Hola a todos,

Tengo un pequeño al problema, estoy cargando en un SqlDataReader el contenido de un procedimiento almacenado, para escribirlo en un archivo txt, el código funciona sin embargo la falla es que lo que se graba en el archivo corresponde del registro 2 hasta el final y no se que pasa con el registro uno, simplemente no lo graba, esto es lo que hace

|02|Sofia||Lopez|Nuñez|12/12/2013|
|03|Alejandro||Quiróz|Chávez|10/12/2013|
|04|Rosa||Ramirez|Pérez|10/12/2013|

y como tendría que quedar

|01|Oscar||Moreno|Alvarez|13/12/2013|
|02|Sofia||Lopez|Nuñez|12/12/2013|
|03|Alejandro||Quiróz|Chávez|10/12/2013|
|04|Rosa||Ramirez|Pérez|10/12/2013|

acá esta el código

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
 
Public Class Form1
 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 
        Dim cn As New SqlConnection("Server=OMORENO;Database=DBVentas;Integrated Security=True;")
        Dim cmd As New SqlCommand("TraerDatos", cn)
        Dim dr As SqlDataReader
 
        cn.Open()
 
        Try
 
            cmd.CommandType = CommandType.StoredProcedure
 
            dr = cmd.ExecuteReader()
 
            Dim strStreamW As Stream
            Dim strStreamWriter As StreamWriter
 
            dr.Read()
 
            Dim FilePath As String = "C:\Users\Oscar Moreno\Desktop\prueba.txt"
 
            strStreamW = File.OpenWrite(FilePath)
            strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.UTF8)
 
            'AGREGANDO LA INFORMACION
 
            While dr.Read()
 
                strStreamWriter.WriteLine("|" & CStr(dr("DNIUsuario")) & "|" & campo1 & "||" & (CStr(dr("ApellidoPaterno"))) & "|" & (CStr(dr("ApellidoMaterno"))) & "|" & (CStr(dr("FechaNacimimento"))) & "|")
 
            End While
 
            strStreamWriter.Close()
            dr.Close()
            cn.Close()
 
        Catch ex As Exception
 
            MsgBox(ex.Message.ToString)
            cn.Close()
 
        End Try
 
    End Sub
End Class

Cualquier ayuda será muy valiosa, saludos.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

primera línea en un txt (SqlDataReader)

Publicado por omar (128 intervenciones) el 18/12/2013 04:16:58
por que no intentas poner
dr(0)
dr(1)
dr(2)


modificar
While dr.Read()

strStreamWriter.WriteLine("|" & CStr(dr("DNIUsuario")) & "|" & campo1 & "||" & (CStr(dr("ApellidoPaterno"))) & "|" & (CStr(dr("ApellidoMaterno"))) & "|" & (CStr(dr("FechaNacimimento"))) & "|")

End While
si todavía tienes problema envía un email
pc.net.2018 en hotmail
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 29
Ha aumentado su posición en 2 puestos en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

primera línea en un txt (SqlDataReader)

Publicado por apacheco (98 intervenciones) el 23/01/2014 15:57:26
Tu error es el siguiente. Cuando haces

dr.Read() ......... Aqui lees el primer registro y lo guardar en .txt

Dim FilePath As String = "C:\Users\Oscar Moreno\Desktop\prueba.txt"

strStreamW = File.OpenWrite(FilePath)
strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.UTF8)

'AGREGANDO LA INFORMACION

While dr.Read() ... aqui lees del 2º registro al últimi y si los grabas en el .txt

Resuelvelo de la siguiente forma

Try

cmd.CommandType = CommandType.StoredProcedure

dr = cmd.ExecuteReader()

Dim strStreamW As Stream
Dim strStreamWriter As StreamWriter

Dim FilePath As String = "C:\Users\Oscar Moreno\Desktop\prueba.txt"

strStreamW = File.OpenWrite(FilePath)
strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.UTF8)

'AGREGANDO LA INFORMACION

While dr.Read()

strStreamWriter.WriteLine("|" & CStr(dr("DNIUsuario")) & "|" & campo1 & "||" & (CStr(dr("ApellidoPaterno"))) & "|" & (CStr(dr("ApellidoMaterno"))) & "|" & (CStr(dr("FechaNacimimento"))) & "|")

End While

strStreamWriter.Close()
dr.Close()
cn.Close()

Catch ex As Exception

MsgBox(ex.Message.ToString)
cn.Close()

End Try

End Sub

Generaras un fichero vacio en caso de no existr registros
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar