
Generacion de archivos planos con resultado de un Stored Procedure en VB.net
Publicado por Monserrat (1 intervención) el 27/08/2018 06:32:59
Requiero hacer unos reportes para mi trabajo en archivos txt en una aplicacion de consola con visual studio 2017 y BD de SQL Server 2008 R2 de manera ciclica a traves de un for each que ejecute un stored procedure.
El la logica del funcionamiento es la siguiente:
En el SP de SQL se realiza una consulta de varias tablas tomando como parametro o ID el nombre del proveedor. En el programa de visual basic dentro de un foreach tiene que ejecutar ese SP y generar un txt con el resultado de ese proveedor. cuando termine de generar ese reporte leera el siguiente nombre de proveedor y repitira el proceso hasta que se terminen los proveedores.
Mi problema es que no me genera el txt, ejecuto el SP en SQL y me muestra lo que quiero pero en el momento de correr el programa no me decuelve nada.
El codigo de VB es:
Agradeceria que me ayudaran ya que llevo dias atorada con este problema y quizas es algo simple y tengo bloqueo tecnico. El problema esta en VB no en el SP
El la logica del funcionamiento es la siguiente:
En el SP de SQL se realiza una consulta de varias tablas tomando como parametro o ID el nombre del proveedor. En el programa de visual basic dentro de un foreach tiene que ejecutar ese SP y generar un txt con el resultado de ese proveedor. cuando termine de generar ese reporte leera el siguiente nombre de proveedor y repitira el proceso hasta que se terminen los proveedores.
Mi problema es que no me genera el txt, ejecuto el SP en SQL y me muestra lo que quiero pero en el momento de correr el programa no me decuelve nada.
El codigo de VB es:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Imports System.Data.SqlClient
Imports System.IO
Module Module1
Public CadCon As String = "data source=172.23.1.117; Database=SIAT; User ID=usertaf;Password=marCtafUser5;integrated security = false"
Public Proveedor As String '= "SELECT IdProv FROM sia.TMN_RepSemProvInven where Activo = 1"
Public SPInv As String = "SPMN_GeneraRepInvSem"
Public SPConsProv As String = "SPMN_GeneRepInvSem"
Public FecCorte As String = "2018-08-20"
Public TVtas As DataTable '= ConInvXProv()
'Public TInv As DataTable = InvXProv(CadCon, FecCorte, Proveedor, datos)
Public TProv As DataTable
Public datos As DataSet
Sub Main()
'CreaTXT()
ConInvXProv()
End Sub
Sub CreaTXT()
'Dim path As String = "C:\Compartir\"
Dim path As String = "C:\Compartir\" & Proveedor & " Inventario.txt", Tvtas ', "|"', True, False
Dim TInv As DataTable '= ConInvXProv() '= InvXProv(CadCon, Proveedor, FecCorte) ', datos)
Dim ResFIn As Boolean = True
If File.Exists(path) = False Then
' Create a file to write to.
For Each row As DataRow In TInv.Rows
Using sw As StreamWriter = File.CreateText(path)
sw.WriteLine(Proveedor)
sw.Flush()
End Using
Next
End If
'' Open the file to read from.
Using sr As StreamReader = File.OpenText(path)
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Loop
End Using
For Each row As DataRow In TProv.Rows
Proveedor = CStr(row("proveedor"))
'Ls_IdsProveedor = CStr(row("supplier_ids"))
TInv = TotalInvPorProveedor().Tables(0) ' yyyy-mm-dd
'tabla_inventario = BlProveedores.ObtenerInventario_PorProveedor(Gs_StrConexion, Ls_IdsProveedor).Tables(0)
Dim bln_ventas As Boolean = CreateTextDelimiterFile("C:\Compartir\" & Proveedor & " Inventario.txt", TInv, "|", True, False)
If bln_ventas = False Then
ResFIn = False
Exit For
End If
Next
End Sub
Sub NomProv()
'("C:\Compartir\" & Proveedor & " Inventario.txt", Tvtas, "|", True, False)
Dim Cadcon As New SqlConnection("data source=172.23.1.117; Database=SIAT; User ID=usertaf;Password=marCtafUser5;integrated security = false")
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Try
Cadcon.Open()
cmd.CommandText = "SELECT IdProv FROM sia.TMN_RepSemProvInven where Activo = 1"
cmd.CommandType = CommandType.Text
cmd.ExecuteScalar()
cmd.Connection = Cadcon
dr = cmd.ExecuteReader
Cadcon.Close()
Catch ex As Exception
MsgBox("Error en la conexion" & ex.Message)
Finally
Cadcon.Close()
End Try
End Sub
Sub ConInvXProv()
Dim Cadcon As New SqlConnection
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Try
Cadcon.Open()
cmd.CommandText = "SELECT * FROM dbo.f_VentasPorProveedor(@FechaInicial, @FechaFinal, @Proveedor)"
cmd.CommandType = CommandType.Text
cmd.ExecuteScalar()
cmd.Connection = Cadcon
dr = cmd.ExecuteReader
Cadcon.Close()
Catch ex As Exception
MsgBox("Error en la conexion" & ex.Message)
Finally
Cadcon.Close()
End Try
End Sub
'Function NomArch(ByVal Proveedor As String, ByVal TVtas As DataTable) As DataSet
' 'Dim dsDatos As DataSet
'End Function
'Public Sub InvXProv(ByVal CadCon As String, ByVal FecCorte As String, ByVal Proveedor As String) ', ByVal datos As DataSet)
Public Sub InvXProv(CadCon, FecCorte, Proveedor) ', ByVal datos As DataSet)
Dim Str(0) As String
Str(0) = "@InvProv"
Dim Parametros(1) As SqlClient.SqlParameter
Parametros(0) = New SqlClient.SqlParameter("@FecCorte", FecCorte)
Parametros(1) = New SqlClient.SqlParameter("@Proveedor", Proveedor)
Dim Program As Object = Nothing
Program.FillDataset(CadCon, CommandType.StoredProcedure, SPInv, Proveedor, Str, Parametros)
End Sub
Sub TotalInvPorProveedor()
Dim CadCon, FecIni, FecFin, Proveedor As String
Dim datos As DataSet
Dim Str(0) As String
Str(0) = "SELECT * FROM dbo.f_VentasPorProveedor(@FechaInicial, @FechaFinal, @Proveedor)"
Dim Parametros(3) As SqlClient.SqlParameter
Parametros(0) = New SqlClient.SqlParameter("@FechaInicial", FecIni)
Parametros(1) = New SqlClient.SqlParameter("@FechaFinal", FecFin)
Parametros(1) = New SqlClient.SqlParameter("@Proveedor", Proveedor)
Dim Program As Object = Nothing
Program.FillDataset(CadCon, CommandType.StoredProcedure, SPConsProv, Proveedor, Str, Parametros)
End Sub
End Module
Agradeceria que me ayudaran ya que llevo dias atorada con este problema y quizas es algo simple y tengo bloqueo tecnico. El problema esta en VB no en el SP
Valora esta pregunta


0