'-------- FUNCIÓN QUE RETORNA UN RECORDSET EN LA CONNEXIÓN A LA BASE DE DATOS A PARTIR DE UNA CONSULTA SQL -------
Sub ConnSQL(ByRef R As ADODB.Recordset, ByRef C As ADODB.Connection, ByVal nomBD As String, ByVal nomSERV As String, ByVal nomUSER As String, ByVal passwUSER As String, ByVal sql As String)
'--------CONECTAR SQLSERVER -------
C = New ADODB.Connection
R = New ADODB.Recordset
C.Open("Provider=SQLOLEDB;Initial Catalog=" & nomBD & ";Data Source=" & nomSERV & ";User ID=" & nomUSER & ";Password=" & passwUSER & ";")
'Otros proveedores BD: http://www.connectionstrings.com/ http://www.dofactory.com/Connect/Connect.aspx#_self12
Try
R = C.Execute(sql)
Catch ex As SqlException
MessageBox.Show(ex.Message)
End Try
End Sub
'-------- RECORRER EL RECORDSET DEVUELTO POR LA ANTERIOR RUTINA PLASMANDOLO EN UN DATAGRIDVIEW-------
Sub llenarDATAGRIDVIEW(ByVal dt As DataGridView)
Dim r As ADODB.Recordset, c As ADODB.Connection
ConnSQL(r, c,"BDEMPRESA","SERVIDOR","sa","emp48", "Select codcli,nomcli,telcli from clientes(NOLOCK) ")
dt.RowCount = 0
If Not r.EOF Then
r.MoveFirst()
While Not r.EOF
dt.Rows.Add()
dt("codigo", dt.RowCount - 1).Value = r.Fields("codart").Value
dt("nombre", dt.RowCount - 1).Value = r.Fields("nomcli").Value
dt("telefono", dt.RowCount - 1).Value = r.Fields("telcli").Value
r.MoveNext()
End While
End If
End Sub
No hay comentarios:
Publicar un comentario