Visual Studio.Net - Sql Server

10,975 views

Published on

Tres formas para manipular registros desde Visual Basic.Net a una Base de Datos en Sql Server.

Published in: Education
3 Comments
39 Likes
Statistics
Notes
No Downloads
Views
Total views
10,975
On SlideShare
0
From Embeds
0
Number of Embeds
196
Actions
Shares
0
Downloads
0
Comments
3
Likes
39
Embeds 0
No embeds

No notes for slide

Visual Studio.Net - Sql Server

  1. 1. http://sistemasddm.blogspot.comtkd.ddm@gmail.comPRIMERA FORMA EN ADO.NETVISUAL.NETImports System.Data.SqlClientPublic Class Form1Dim CN As New SqlConnection("data source= . ; database = Northwind ; integrated security = true")Dim DS As New DataSetDim DA As SqlDataAdapterDim BUSCAR As IntegerPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.LoadDA = New SqlDataAdapter("select * from Employees", CN)DS = New DataSetDA.Fill(DS, "Employees")Me.DataGridView1.DataSource = DS.Tables("Employees")End SubPrivate Sub btnnuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handlesbtnnuevo.ClickMe.txtbuscar.Enabled = TrueMe.txtbuscar.Text = ""End Sub
  2. 2. http://sistemasddm.blogspot.comtkd.ddm@gmail.comPrivate Sub btnbuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handlesbtnbuscar.ClickDim objdataview As New DataViewDim buscar As IntegerMe.DataGridView1.DataSource = Nothingbuscar = Me.txtbuscar.Textobjdataview.Table = DS.Tables("Employees")objdataview.RowFilter = "employeeid= " & buscar & ""If objdataview.Count > 0 ThenMe.DataGridView1.DataSource = objdataviewMe.DataGridView1.Refresh()ElseMsgBox("registro no encontrado", MsgBoxStyle.Information, "advertencia")End IfMe.txtbuscar.Enabled = FalseEnd SubEnd Class
  3. 3. http://sistemasddm.blogspot.comtkd.ddm@gmail.comSEGUNDA FORMA EN ADO.NETVISUAL BASIC.NETUSE MASTERCREATE TABLE EMPLEADOS(CODIGO NVARCHAR(5),NOMBRE NVARCHAR(25),APELLIDOS NVARCHAR (25),DIRECCION NVARCHAR (25),TELEFONO NVARCHAR (25))INSERT INTO EMPLEADOSVALUES (E0001, RICHARD, RAMOS BARRIOS , LA VISTORIA, 997798005)INSERT INTO EMPLEADOSVALUES (E0002, JUAN , CARLOS ROJAS , MOLINA, 997798008)INSERT INTO EMPLEADOSVALUES (E0003, CARLOS, LOLO CARLOS , COMAS, 997798007)INSERT INTO EMPLEADOSVALUES (E0004, NILDA, DURAND MORILLO , LIMA, 997798009)Imports System.Data.SqlClientPublic Class Form1Dim CN As New SqlConnection("data source= . ;user id = 123 ; database = master ; integrated security= true")
  4. 4. http://sistemasddm.blogspot.comtkd.ddm@gmail.comDim DS As New DataSetDim DA As SqlDataAdapterDim dW As New DataViewPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.LoadDA = New SqlDataAdapter("SELECT * FROM EMPLEADOS", CN)DS = New DataSetDA.Fill(DS, "EMPLEADOS")Me.DataGridView1.DataSource = DS.Tables("EMPLEADOS")End SubPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handlesbtnnuevo.ClickDim ctr As ControlFor Each ctr In Me.ControlsIf TypeOf ctr Is TextBox Then ctr.Text = ""Me.DataGridView1.DataSource = NothingNextEnd SubPrivate Sub btnbuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handlesbtnbuscar.ClickMe.DataGridView1.DataSource = NothingdW.Table = DS.Tables("empleados")dW.RowFilter = "codigo = " & txtcodigo.Text & ""If dW.Count > 0 ThenMe.DataGridView1.DataSource = dWMe.DataGridView1.Refresh()Me.txtcodigo.Text = Me.DataGridView1.Item(0, 0).ValueMe.txtnombre.Text = Me.DataGridView1.Item(1, 0).ValueMe.txtapellidos.Text = Me.DataGridView1.Item(2, 0).ValueMe.txtdireccion.Text = Me.DataGridView1.Item(3, 0).ValueMe.txttelefono.Text = Me.DataGridView1.Item(4, 0).ValueElseMsgBox("REGISTRO NO ENCONTRADO")End IfEnd SubEnd Class
  5. 5. http://sistemasddm.blogspot.comtkd.ddm@gmail.comTERCERA FORMA ADO.NETVISUAL BASIC.NETUSE MASTERCREATE TABLE EMPLEADOS(CODIGO NVARCHAR(5),NOMBRE NVARCHAR(25),APELLIDOS NVARCHAR (25),DIRECCION NVARCHAR (25),TELEFONO NVARCHAR (25))INSERT INTO EMPLEADOS VALUES (E0001, RICHARD , RAMOS BARRIOS , LA VISTORIA,997798005)INSERT INTO EMPLEADOS VALUES (E0002, JUAN , CARLOS ROJAS , MOLINA, 997798008)INSERT INTO EMPLEADOS VALUES (E0003, CARLOS , LOLO CARLOS , COMAS, 997798007)INSERT INTO EMPLEADOS VALUES (E0004, NILDA , DURAND MORILLO , LIMA, 997798009)select * from empleados
  6. 6. http://sistemasddm.blogspot.comtkd.ddm@gmail.com
  7. 7. http://sistemasddm.blogspot.comtkd.ddm@gmail.comImports System.Data.SqlClientPublic Class form1Dim CN As New SqlConnection("data source= . ; database = MASTER; integrated security = true")Dim cmd As SqlCommandDim dr As SqlDataReaderPrivate Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handlesbtnsalir.ClickEndEnd SubPrivate Sub btnnuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handlesbtnnuevo.ClickDim ctrl As ControlFor Each ctrl In Me.ControlsIf TypeOf ctrl Is TextBox Then ctrl.Text = ""NextMe.btnnuevo.Text = "buscar"End SubPrivate Sub btnbuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handlesbtnbuscar.ClickTryIf btnbuscar.Text = "buscar" ThenMe.btnbuscar.Text = "buscar ahora"Me.txtcodigo.Enabled = TrueMe.txtcodigo.Focus()Elsecmd = New SqlCommand("select * from empleados where codigo = " & Me.txtcodigo.Text & "", CN)CN.Open()dr = cmd.ExecuteReaderIf dr.Read ThenMe.txtnombre.Text = dr("nombre")Me.txtapellidos.Text = dr("apellidos")Me.txtdireccion.Text = dr("direccion")Me.txttelefono.Text = dr("telefono")ElseMessageBox.Show("el codigo no existe", "error", MessageBoxButtons.OK,MessageBoxIcon.Information)End IfCN.Close()End IfCatch ex As ExceptionMessageBox.Show(ex.Message)End TryEnd SubEnd Class
  8. 8. http://sistemasddm.blogspot.comtkd.ddm@gmail.com

×