Visual Basic.NET - DBHelper para VB 2005

   
Vista:

DBHelper para VB 2005

Publicado por carlos revilla (2 intervenciones) el 04/06/2009 19:47:14
Buenas amigos, quisiera saber si alguno de los excelentes programadores q hay en este foro, tiene o a migrado el DBHelper, a la version de Visual Basic 2005 con las nuevas formas de programar, ya q el ADO, no funciona. les mando un ejemplo de "collectParams"

Private Sub collectParams(ByRef cmd As SqlCommand, ByVal argparams As Object)
Dim params, v As Object
Dim l, i, u As Short

Dim sqldatainput As New SqlDbType

'if argparams is empty
If Not IsArray(argparams) Then Exit Sub
params = argparams
For i = LBound(params) To UBound(params)
l = LBound(params(i))
u = UBound(params(i))
' Check for nulls.
If u - l = 3 Then
If VarType(params(i)(3)) = VariantType.String Then
v = IIf(params(i)(3) = "", System.DBNull.Value, params(i)(3))
Else
v = params(i)(3)
End If
cmd.Parameters.(params(i)(0), params(i)(1), ??????, params(i)(2), v)
End If
Next i
End Sub

---------------------------------------------------------------------------------------------------

Sub RunSP(ByVal strSP As String, ByRef params As Object)
Dim conecta As New SqlConnection(getconeccion)
Dim cmd As New SqlCommand
conecta.Open()
cmd.Connection = conecta
cmd.CommandText = strSP
cmd.CommandType = CommandType.StoredProcedure
collectParams(cmd, params)

cmd.ExecuteReader()
conecta.Close()
cmd = Nothing
End Sub

--------------------------------------------------------------------------------------------

Function getconeccion() As String
Return "data source=192.168.11.16;initial catalog=prueba;User id=sa; password=123;integrated security=false"
End Function

------------------------------------------------------------------------------------------------

De donde lo llamo modulo

Sub graba_add(ByVal codigo As Int16, ByVal nombre As String, ByVal fono As String)
m_dbh.RunSP("sp_add_datos", New Object() {mp("@id_ctoper", SqlDbType.Int, 3, codigo), mp("@ccod_cia", SqlDbType.Char, 15, nombre), mp("@ccod_oper_log", SqlDbType.NVarChar, 15, fono)})
End Sub

------------------------------------------------------------------------------------------------

Public Function mp(ByVal PName As String, ByVal PType As SqlDbType, ByVal PSize As Long, ByVal PValue As Object) As Object
mp = New Object() {PName, PType, PSize, PValue}
End Function
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

RE:DBHelper para VB 2005

Publicado por P. J. (706 intervenciones) el 06/06/2009 20:55:21
Pues no he usado el DBHelper, pero para buenas practicas puedes usar el SQLHelper aunque hay librerias mas actualizadas por ahora sigo usando esa clase para manejar mi acceso a datos.

Saludos.
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

RE:DBHelper para VB 2005

Publicado por carlos revilla (2 intervenciones) el 08/06/2009 20:21:12
Function getconeccion() As String
Return "data source=192.168.14.16;initial catalog=prueba;User id=sa; password=123456;integrated security=false"
End Function
'Elaborado por Carlos Tigre lider Revilla
'Public Function RunSPreturnRS(ByVal strSP As String) As DataSet
' Dim conecta As New SqlConnection(getconeccion)
' Dim dataset6 As New DataSet
' Dim adapter As New SqlDataAdapter()
' conecta.Open()
' adapter.SelectCommand = New SqlCommand(strSP, conecta)
' adapter.Fill(dataset6)
' adapter = Nothing
' conecta.Close()
' dataset6.Dispose()
' Return dataset6
'End Function
'ejecuta procedimiento con parametros
Public Function RunSPreturnRS(ByVal strSP As String, Optional ByRef params As Object = Nothing) As DataSet
'Lista todos los registros y/o busca registros
Dim conecta As New SqlConnection(getconeccion)
Dim cmd As New SqlCommand
Dim result As New DataSet
Dim adapter As New SqlDataAdapter
conecta.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = strSP
cmd.CommandTimeout = 0
cmd.Connection = conecta
collectParams(cmd, params)
adapter.SelectCommand = cmd
adapter.Fill(result)
conecta = Nothing
adapter = Nothing
cmd.Dispose()
Return result
End Function

Public Function RunSPreturnInteger(ByVal strSP As String, Optional ByRef params As Object = Nothing) As Int16
'Retorna un valor entero desde la base de datos
Dim conecta As New SqlConnection(getconeccion)
Dim cmd As New SqlCommand
Dim entero As Int16
conecta.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = strSP
cmd.CommandTimeout = 0
cmd.Connection = conecta
collectParams(cmd, params)
entero = cmd.ExecuteScalar
conecta = Nothing
cmd.Dispose()
Return entero

End Function

Sub RunSP(ByVal strSP As String, ByRef params As Object)
Dim conecta As New SqlConnection(getconeccion)
Dim cmd As New SqlCommand
conecta.Open()
cmd.Connection = conecta
cmd.CommandText = strSP
cmd.CommandType = CommandType.StoredProcedure
collectParams(cmd, params)
cmd.ExecuteNonQuery()
conecta.Close()
cmd = Nothing
End Sub

Private Sub collectParams(ByRef cmd As SqlCommand, ByVal argparams As Object)
Dim params, v As Object
Dim l, i, u As Short
Dim sqldatainput As New SqlDbType
'if argparams is empty
If Not IsArray(argparams) Then Exit Sub
params = argparams
For i = LBound(params) To UBound(params)
l = LBound(params(i))
u = UBound(params(i))
' Check for nulls.
If u - l = 3 Then
If VarType(params(i)(3)) = VariantType.String Then
v = IIf(params(i)(3) = "", System.DBNull.Value, params(i)(3))
Else
v = params(i)(3)
End If
cmd.Parameters.Add(params(i)(0), params(i)(1), params(i)(2), params(i)(3)).Value = (v.ToString)
End If
Next i
End Sub

Yo mismo lo resolvi y funciona Sr's.
Espero q les sirva
chau
heycarlito@peru.com
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