24/11/2005, 09:55
|
| | Fecha de Ingreso: noviembre-2005
Mensajes: 9
Antigüedad: 19 años Puntos: 0 | |
aqui va un ejemplo Public Class Form1
Inherits System.Windows.Forms.Form
Private oConexion As SqlConnection
Private oDataSet As DataSet
Private oDataAdapter As SqlDataAdapter
#Region " Código generado por el Diseñador de Windows Forms "
Public Sub New()
MyBase.New()
'El Diseñador de Windows Forms requiere esta llamada.
InitializeComponent()
'Agregar cualquier inicialización después de la llamada a InitializeComponent()
End Sub
'Form reemplaza a Dispose para limpiar la lista de componentes.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Requerido por el Diseñador de Windows Forms
Private components As System.ComponentModel.IContainer
'NOTA: el Diseñador de Windows Forms requiere el siguiente procedimiento
'Puede modificarse utilizando el Diseñador de Windows Forms.
'No lo modifique con el editor de código.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents btnGrabar As System.Windows.Forms.Button
Friend WithEvents txtIDAutor As System.Windows.Forms.TextBox
Friend WithEvents txtAutor As System.Windows.Forms.TextBox
Friend WithEvents grdDatos As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.txtIDAutor = New System.Windows.Forms.TextBox
Me.txtAutor = New System.Windows.Forms.TextBox
Me.btnGrabar = New System.Windows.Forms.Button
Me.grdDatos = New System.Windows.Forms.DataGrid
CType(Me.grdDatos, System.ComponentModel.ISupportInitialize).BeginIni t()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 40)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(56, 24)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Label1"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(24, 72)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(56, 24)
Me.Label2.TabIndex = 1
Me.Label2.Text = "Label2"
'
'txtIDAutor
'
Me.txtIDAutor.Location = New System.Drawing.Point(88, 40)
Me.txtIDAutor.Name = "txtIDAutor"
Me.txtIDAutor.Size = New System.Drawing.Size(112, 20)
Me.txtIDAutor.TabIndex = 2
Me.txtIDAutor.Text = ""
'
'txtAutor
'
Me.txtAutor.Location = New System.Drawing.Point(88, 72)
Me.txtAutor.Name = "txtAutor"
Me.txtAutor.Size = New System.Drawing.Size(112, 20)
Me.txtAutor.TabIndex = 3
Me.txtAutor.Text = ""
'
'btnGrabar
'
Me.btnGrabar.Location = New System.Drawing.Point(264, 32)
Me.btnGrabar.Name = "btnGrabar"
Me.btnGrabar.Size = New System.Drawing.Size(96, 24)
Me.btnGrabar.TabIndex = 4
Me.btnGrabar.Text = "Grabar"
'
'grdDatos
'
Me.grdDatos.DataMember = ""
Me.grdDatos.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.grdDatos.Location = New System.Drawing.Point(32, 120)
Me.grdDatos.Name = "grdDatos"
Me.grdDatos.Size = New System.Drawing.Size(320, 120)
Me.grdDatos.TabIndex = 5
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(392, 281)
Me.Controls.Add(Me.grdDatos)
Me.Controls.Add(Me.btnGrabar)
Me.Controls.Add(Me.txtAutor)
Me.Controls.Add(Me.txtIDAutor)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.grdDatos, System.ComponentModel.ISupportInitialize).EndInit( )
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' crear conexión
oConexion = New SqlConnection
oConexion.ConnectionString = "Server=(local);Database=MUSICA;uid=sa;pwd=;"
' crear adaptador
oDataAdapter = New SqlDataAdapter
' crear comandos para inserción, consulta con sus parámetros
' y asignarlos al adaptador
Dim oCmdInsercion As New SqlCommand("INSERT INTO AUTORES " & _
"(IDAutor,Autor) VALUES(@IDAutor,@Autor)", oConexion)
oDataAdapter.InsertCommand = oCmdInsercion
oDataAdapter.InsertCommand.Parameters.Add(New SqlParameter("@IDAutor", SqlDbType.Int))
oDataAdapter.InsertCommand.Parameters.Add(New SqlParameter("@Autor", SqlDbType.NVarChar))
Dim oCmdConsulta As New SqlCommand("SELECT * FROM AUTORES", _
oConexion)
oDataAdapter.SelectCommand = oCmdConsulta
' crear conjunto de datos
oDataSet = New DataSet
Me.CargarDatos()
End Sub
Private Sub CargarDatos()
' vaciar el dataset
oDataSet.Clear()
oConexion.Open() ' abrir conexión
' utilizar el adaptador para llenar el dataset con una tabla
oDataAdapter.Fill(oDataSet, "Autores")
oConexion.Close() ' cerrar conexión
' enlazar dataset con datagrid;
' en DataSource se asigna el dataset,
' en DataMember el nombre de la tabla del
' dataset que mostrará el datagrid
Me.grdDatos.DataSource = oDataSet
Me.grdDatos.DataMember = "Autores"
End Sub
Private Sub btnGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrabar.Click
Dim iResultado As Integer
' asignar valores a los parámetros para el
' comando de inserción
oDataAdapter.InsertCommand.Parameters("@IDAutor"). Value = Me.txtIDAutor.Text
oDataAdapter.InsertCommand.Parameters("@Autor").Va lue = Me.txtAutor.Text
' abrir conexión
oConexion.Open()
' ejecutar comando de inserción del adaptador
iResultado = oDataAdapter.InsertCommand.ExecuteNonQuery()
' cerrar conexión
oConexion.Close()
Me.CargarDatos()
MessageBox.Show("Registros añadidos: " & iResultado)
End Sub
End Class |