|
-
Aug 20th, 2006, 07:03 PM
#1
Thread Starter
New Member
Need convert this code to 2005 anyone can help?
this code work in .net 1 ,but not work in .net 2 .i conver with the 2005 but the wizard run the app but no change the code if anyone can help me?
can correct some line but i new , for example
i can correct Private da As OleDbDataAdapter to Private da As OleDb.OleDbDataAdapter
this is the code:
Option Strict On
Imports Microsoft.VisualBasic
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
Application.EnableVisualStyles()
Application.DoEvents()
'El Diseñador de Windows Forms requiere esta llamada.
InitializeComponent()
'Agregar cualquier inicialización después de la llamada a InitializeComponent()
End Sub
' Las variables que usaremos en este ejemplo
Private dt As DataTable
Private da As OleDbDataAdapter
Private fila As Integer
'
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' Limpiar los controles del GroupBox y
' deshabilitarlos hasta que se conecte a la base de datos
'
For Each c As Control In Me.GroupBox1.Controls
' Limpiar los textbox
If TypeOf c Is TextBox Then
c.Text = ""
End If
' Deshabilitarlos
c.Enabled = False
Next
Me.GroupBox1.Enabled = False
Me.GroupBox1.Text = "Debes conectar antes de usar los datos"
'
' El nombre de la base de datos:
' (poner el path real de la base de datos de prueba)
Me.txtBase.Text = "E:\gsCodigo_00\VS.NET\vb y cs\acceso a datos\ejemplo_elGuille\db2000.mdb"
End Sub
Private Sub btnConectar_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnConectar.Click
' Conectar y mostrar los datos
' Comprobar si existe la bse de datos
Try
If System.IO.File.Exists(txtBase.Text) = False Then
MessageBox.Show("No existe la base de datos indicada")
txtBase.Focus()
Exit Sub
End If
Catch ex As Exception
MessageBox.Show("ERROR: " & ex.Message & vbCrLf & "Seguramente porque no existe la base de datos indicada")
txtBase.Focus()
Exit Sub
End Try
'
' La cadena de conexión
Dim sCnn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & txtBase.Text
' La cadena de selección
Dim sSel As String = "SELECT * FROM Prueba ORDER BY ID"
' Para traer solo los registros entre dos fechas
' sSel = "SELECT * FROM Prueba WHERE (FechaAlta >= #2006/01/05# AND FechaAlta <= #2006/01/06#)"
'
' Comprobar si hay algún error
Try
' Crear un nuevo objeto del tipo DataAdapter
'Dim cnn As New OleDbConnection(sCnn)
da = New OleDbDataAdapter(sSel, sCnn)
' Crear los comandos de insertar, actualizar y eliminar
Dim cb As New OleDbCommandBuilder(da)
' Como hay campos con caracteres especiales,
' al usarlos incluirlos entre corchetes.
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
' Asignar los comandos al DataAdapter
' (se supone que lo hace automáticamente, pero...)
da.UpdateCommand = cb.GetUpdateCommand
da.InsertCommand = cb.GetInsertCommand
da.DeleteCommand = cb.GetDeleteCommand
'
' Esta base de datos usa el ID con valores automáticos
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
'
dt = New DataTable
' Llenar la tabla con los datos indicados
da.Fill(dt)
'
' Habilitar los controles
For Each c As Control In Me.GroupBox1.Controls
c.Enabled = True
Next
Me.GroupBox1.Enabled = True
Me.GroupBox1.Text = "Conexión realizada"
' Y mostrar el primer registro
If dt.Rows.Count > 0 Then
btnFirst_Click(Nothing, Nothing)
Else
fila = -1
btnActualizar.Enabled = False
End If
Catch ex As Exception
MessageBox.Show("ERROR al conectar o recuperar los datos:" & vbCrLf & _
ex.Message, "Conectar con la base", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub asignarDatos(ByVal dr As DataRow)
' Usar los datos que hay en los textbox
dr("Nombre") = txtNombre.Text
dr("e-mail") = txtEmail.Text
dr("FechaAlta") = txtFechaAlta.Text
dr("Comentario") = txtComentario.Text
End Sub
Private Sub mostrarDatos(ByVal f As Integer)
Dim uf As Integer = dt.Rows.Count - 1
If f < 0 OrElse uf < 0 Then Exit Sub
'
Dim dr As DataRow = dt.Rows(f)
txtID.Text = dr("ID").ToString
txtNombre.Text = dr("Nombre").ToString
txtEmail.Text = dr("e-mail").ToString
txtFechaAlta.Text = dr("FechaAlta").ToString
txtComentario.Text = dr("Comentario").ToString
'
btnActualizar.Enabled = True
End Sub
Private Sub btnFirst_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnFirst.Click
' Posicionarse en la primera fila
fila = 0
' Mostrar los datos de la fila indicada
mostrarDatos(fila)
End Sub
Private Sub btnPrev_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnPrev.Click
' Posicionarse en la fila anterior
fila = fila - 1
If fila < 0 Then fila = 0
' Mostrar los datos de la fila indicada
mostrarDatos(fila)
End Sub
Private Sub btnNext_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnNext.Click
' Posicionarse en la fila siguiente
Dim uf As Integer = dt.Rows.Count - 1
fila = fila + 1
If fila > uf Then fila = uf
' Mostrar los datos de la fila indicada
mostrarDatos(fila)
End Sub
Private Sub btnLast_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnLast.Click
' Posicionarse en la última fila
fila = dt.Rows.Count - 1
' Mostrar los datos de la fila indicada
mostrarDatos(fila)
End Sub
Private Sub btnActualizar_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnActualizar.Click
' Actualizar los datos en la fila actual
If fila < 0 OrElse fila > dt.Rows.Count - 1 Then Exit Sub
Dim dr As DataRow = dt.Rows(fila)
' Asignar los datos de los textbox a la fila
asignarDatos(dr)
'dr("Nombre") = txtNombre.Text
'dr("e-mail") = txtEmail.Text
'dr("FechaAlta") = txtFechaAlta.Text
'dr("Comentario") = txtComentario.Text
' Guardar físicamente los datos en la base
Try
da.Update(dt)
dt.AcceptChanges()
Catch ex As DBConcurrencyException
MessageBox.Show("Error de concurrencia:" & vbCrLf & ex.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub btnNuevo_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnNuevo.Click
' Crear un nuevo registro
Dim dr As DataRow = dt.NewRow()
' Asignar los datos de los textbox a la fila
asignarDatos(dr)
'dr("Nombre") = txtNombre.Text
'dr("e-mail") = txtEmail.Text
'dr("FechaAlta") = txtFechaAlta.Text
'dr("Comentario") = txtComentario.Text
' Añadir la nueva fila a la tabla
dt.Rows.Add(dr)
' Guardar físicamente los datos en la base
Try
da.Update(dt)
dt.AcceptChanges()
' Si es el primer registro de la base,
' volver a leer los datos para actualizar los IDs
If CInt("0" & dr("ID").ToString) = 0 Then
dt = New DataTable
da.Fill(dt)
End If
' Posicionarlo en la última fila
btnLast_Click(Nothing, Nothing)
Catch ex As DBConcurrencyException
MessageBox.Show("Error de concurrencia:" & vbCrLf & ex.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub btnExaminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExaminar.Click
' Seleccionar una base de datos
' (aunque debe tener la tabla y campos usados en este ejemplo)
Dim ofd As New OpenFileDialog
ofd.Title = "Seleccionar base de datos"
ofd.FileName = txtBase.Text
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
txtBase.Text = ofd.FileName
End If
End Sub
End Class
and i have the asembly if any need!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|