Está en la página 1de 3

'-----------------------------------------------------------------------------' Instrucciones SQL desde ADO.

NET con Visual Basic y SQL Server (25/May/08) ' ' Guillermo 'guille' Som, 2008 '-----------------------------------------------------------------------------Option Strict On Imports Microsoft.VisualBasic Imports System Imports System.Data Imports System.Data.SqlClient Public Class AccesoDatosSQL Public NombreTabla As String = "historiarecibos" Public Function CadenaConexion() As String Dim csb As New SqlConnectionStringBuilder csb.DataSource = "(local)\SQLEXPRESS" csb.InitialCatalog = "simsql" csb.IntegratedSecurity = True Return csb.ConnectionString End Function Public Function Insertar(ByVal nombre As String, _ ByVal Direccion As String, _ ByVal Folio As Integer, _ ByVal Fecha_pago As DateTime, _ ByVal Impo_totalRecibo As Long) As Integer Dim sCon As String = CadenaConexion() Dim sel As String MsgBox("Perchero") sel = "INSERT INTO " & NombreTabla & _ "(Nombre, Direccion, Folio, Fecha_pago, Impo_totalRecibo) " & _ "VALUES " & _ "(@Nombre, @Direccion, @Folio, @Fecha_pago, @Impo_totalRecibo) " & _ "SELECT @@Identity" Using con As New SqlConnection(sCon) Dim cmd As New SqlCommand(sel, con) cmd.Parameters.AddWithValue("@Nombre", nombre) cmd.Parameters.AddWithValue("@Direccion", Direccion) cmd.Parameters.AddWithValue("@Folio", Folio) cmd.Parameters.AddWithValue("@Fecha_pago", Fecha_pago) cmd.Parameters.AddWithValue("@Impo_totalRecibo", Impo_totalRecibo) con.Open() Dim t As Integer = CInt(cmd.ExecuteScalar()) con.Close() Return t End Using End Function Public Function InsertarAdapter(ByVal nombre As String, _ ByVal apellidos As String, _ ByVal email As String, _ ByVal fecha As DateTime, _ ByVal descripcion As String) As Integer Dim sCon As String = CadenaConexion()

Dim sel As String ' Usando un DataAdapter sel = "SELECT * FROM " & NombreTabla Dim da As New SqlDataAdapter(sel, sCon) da.MissingSchemaAction = MissingSchemaAction.AddWithKey Dim cb As New SqlCommandBuilder(da) Dim dt As New DataTable da.Fill(dt) Dim dr As DataRow = dt.NewRow dr("Nombre") = nombre dr("Apellidos") = apellidos dr("Email") = email dr("Fecha") = fecha.ToString("dd/MM/yyyy") dr("Descripcion") = descripcion dt.Rows.Add(dr) da.Update(dt) Return CInt(dr("ID")) End Function Public Sub Eliminar(ByVal id As Integer) Dim sCon As String = CadenaConexion() Dim sel As String = "DELETE FROM " & NombreTabla & " WHERE ID = @ID" Using con As New SqlConnection(sCon) Dim cmd As New SqlCommand(sel, con) cmd.Parameters.AddWithValue("@ID", id) con.Open() cmd.ExecuteNonQuery() con.Close() End Using End Sub Public Sub Actualizar(ByVal id As Integer, _ ByVal nombre As String, _ ByVal Direccion As String, _ ByVal Folio As String, _ ByVal Fecha_pago As DateTime) Dim sCon As String = CadenaConexion() Dim sel As String sel = "UPDATE " & NombreTabla & _ " SET Nombre = @Nombre, Direccion = @Apellidos, Folio = @Email, Fecha_pago = @Fecha" & _ " WHERE ID = @ID" Using con As New SqlConnection(sCon) Dim cmd As New SqlCommand(sel, con) cmd.Parameters.AddWithValue("@ID", id) cmd.Parameters.AddWithValue("@Nombre", nombre)

cmd.Parameters.AddWithValue("@Direccion", apellidos) cmd.Parameters.AddWithValue("@Folio", email) cmd.Parameters.AddWithValue("@Fecha_pago", fecha) 'cmd.Parameters.AddWithValue("@Descripcion", descripcion) con.Open() Dim t As Integer = cmd.ExecuteNonQuery() con.Close() Console.WriteLine("Filas actualizadas: {0}", t) End Using End Sub Public Function TotalRegistros() As Integer Dim sCon As String = CadenaConexion() Dim sel As String = "SELECT COUNT(*) FROM " & NombreTabla Using con As New SqlConnection(sCon) Dim cmd As New SqlCommand(sel, con) con.Open() Dim t As Integer = CInt(cmd.ExecuteScalar()) con.Close() Return t End Using End Function End Class