InicioInfoautorefresh dataset vb.net mysql

autorefresh dataset vb.net mysql

Info9/17/2011
Hace un par de dias que me encontre con esta situacion el que los datos del dataset solo son los datos que el servidor te envio atraves del dataadacter cuando se hiso el fill

ejemplo

Dim ds as new dataset
Dim da As New MySqlDataAdapter("Select * from prueba", cn)
da.Fill(ds, "prueba" )

ahora bien que pasa si la informacion desplegada nesecitamos que sea en real time (como en la ventas de boletos par abordar un autobus o algo parecido) tendriamos que limpiar el dataset y luego volver a llenar

ejemplo
da = New MySqlDataAdapter("Select * from prueba", cn)
ds.Clear()
da.Fill(ds, "prueba" )

pues bien funciona pero dentro de los problemas es que el datagridview retorna a la primera posicion
y que si la tabla tiene una gran cantidad de datos esto carga mucho la memoria y la red

mi pregunta era hay un sistema que solo afecte a los registros insertado,modificados y eliminados
pero por desgracia tendriamos que tener un evento desde mysql que nos indicara esto y no creo que exista.

asi que comenze la busqueda y encontre algo pero para sql server SqlDependency pero para mysql
no hay nada parecido bueno un mysqlDependency pero por un provedor por paga. no me queda de
otra que buscar la forma de intentar y vi los disparadores de msyql (TRIGGERS) asi que hise la siguiente
clase


Imports MySql.Data.MySqlClient

Public Class mysqlseguimiento
Private _tabla As String
Private _ds As New Data.DataSet
Private _da As MySqlDataAdapter
Private _cn As MySqlConnection
Private _com As MySqlCommand
Private _disparadorname As String
Private _conecion As String
Private _campoprimario As String
Private _tipocampo As String
Private _cantida As Integer = 0
Public Property campoprimario As String
Get
Return _campoprimario
End Get
Set(value As String)
_campoprimario = value
End Set
End Property
Public Property disparador As String
Get
Return _disparadorname
End Get
Set(value As String)
_disparadorname = value
End Set
End Property
Public Property tabla As String
Get
Return _tabla
End Get
Set(value As String)
_tabla = value
End Set
End Property
Public Property tipocampo As String
Get
Return _tipocampo
End Get
Set(value As String)
_tipocampo = value
End Set
End Property
Public Property conecion As String
Get
Return _conecion
End Get
Set(value As String)
_conecion = value
End Set
End Property
Public Property ds As Data.DataSet
Get
Return _ds
End Get
Set(value As Data.DataSet)
_ds = value
End Set
End Property
Sub New()
End Sub
Public Sub iniciseg()

Try

_cn = New MySqlConnection(_conecion)
_cn.Open()
'hacer correcion para comprobar si la tabla ya tiene disparadores!!!!
'
'
'crea la tabla de seguimiento
_com = New MySqlCommand(
"CREATE TABLE IF NOT EXISTS seg" + _disparadorname +
" (idSEG" + _disparadorname + " INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
_campoprimario + " " + _tipocampo + "," +
"fecha datetime default null," +
"accion varchar(45) default null);", _cn
)
_com.ExecuteNonQuery()
'crea el disparadores
_com = New MySqlCommand(
"DROP TRIGGER IF EXISTS RANGER.testref; CREATE TRIGGER testref AFTER INSERT ON " + _tabla +
" FOR EACH ROW BEGIN " +
" INSERT INTO seg" + _disparadorname +
" set " + _campoprimario + "=NEW." + _campoprimario + "," +
"fecha = now(),accion='INSERT';" +
"END ", _cn
)
_com.ExecuteNonQuery()
_com = New MySqlCommand(
"DROP TRIGGER IF EXISTS testref1;CREATE TRIGGER testref1 AFTER UPDATE ON " + _tabla +
" FOR EACH ROW BEGIN " +
" INSERT INTO seg" + _disparadorname +
" set " + _campoprimario + "=OLD." + _campoprimario + "," +
"fecha = now(),accion='UPDATE';" +
"END ", _cn
)
_com.ExecuteNonQuery()
_com = New MySqlCommand(
"DROP TRIGGER IF EXISTS testref2;CREATE TRIGGER testref2 BEFORE DELETE ON " + _tabla +
" FOR EACH ROW BEGIN " +
" INSERT INTO seg" + _disparadorname +
" set " + _campoprimario + "=OLD." + _campoprimario + "," +
"fecha = now(),accion='DELETE';" +
"END ", _cn
)
_com.ExecuteNonQuery()
Catch E As Exception
MsgBox(E.Message)
End Try

End Sub

Public Sub SEGIMIENTO()
Dim tempds As New Data.DataSet
Dim tempds2 As New Data.DataSet
Dim tempda As MySqlDataAdapter
Dim i As Integer
Dim ii As Integer
Dim row As Integer
Dim aborrar As String = "("
_cn = New MySqlConnection(_conecion)
_cn.Open()
Dim ahora As System.DateTime = Now()
Dim ahorita As System.DateTime = DateAdd(DateInterval.Second, -30, ahora)
_da = New MySqlDataAdapter("select * from seg" + _disparadorname + " where fecha>='" + Format(ahorita, "yyyy-MM-dd HH:mm:ss" +
"' and fecha<='" + Format(ahora, "yyyy-MM-dd HH:mm:ss" + "';", _cn)
_da.Fill(tempds)
If tempds.Tables(0).Rows.Count > 0 Then
'por cada registro en la tabla seg actulizo un registro en _ds
For i = 0 To tempds.Tables(0).Rows.Count - 1
'aborrar += tempds.Tables(0).Rows(i).Item("idSEG" + _disparadorname).ToString + ","
'busca la actulizacion en la tabla en el servidor
If tempds.Tables(0).Rows(i).Item("accion" <> "DELETE" Then
tempda = New MySqlDataAdapter(
"select * from " + _tabla +
" where " + _campoprimario + "=" + tempds.Tables(0).Rows(i).Item(_campoprimario).ToString,
_cn
)
tempda.Fill(tempds2)
'busca el registro en ds para actulizarlo
End If
row = _ds.Tables(0).Rows.IndexOf(
_ds.Tables(0).Rows.Find(
tempds.Tables(0).Rows(i).Item(_campoprimario)
)
)

Select Case tempds.Tables(0).Rows(i).Item("accion"
Case "INSERT"
'insert un nuevo registro
'BUSCA EL REGITRO EN EL DATASET para verifica que no exista
If row < 0 Then
row = _ds.Tables(0).Rows.Count

_ds.Tables(0).Rows.Add()

_ds.Tables(0).Rows(row).BeginEdit()
For ii = 1 To _ds.Tables(0).Columns.Count - 1
_ds.Tables(0).Rows(row).Item(_ds.Tables(0).Columns(ii).ColumnName) = tempds2.Tables(0).Rows(0).Item(_ds.Tables(0).Columns(ii).ColumnName)
Next
_ds.Tables(0).Rows(row).EndEdit()
End If
Case "UPDATE"
'ACTUALIZA UN REGISTRO EXISTENTE

'EDITA EL DATABASET
'? SERA POSIBLE ACTULIZAR EL ROW COMPLETO
If row > 0 Then
_ds.Tables(0).Rows(row).BeginEdit()
For ii = 1 To _ds.Tables(0).Columns.Count - 1
_ds.Tables(0).Rows(row).Item(_ds.Tables(0).Columns(ii).ColumnName) = tempds2.Tables(0).Rows(0).Item(_ds.Tables(0).Columns(ii).ColumnName)
Next
_ds.Tables(0).Rows(row).EndEdit()
End If
Case "DELETE"
If row > 0 Then
_ds.Tables(0).Rows(row).Delete()
End If
End Select
Next
End If

End Sub
Private Function existet(ByVal t As String) As Boolean
Dim result As Integer
_com = New MySqlCommand("select * from " + t, _cn)
result = _com.ExecuteNonQuery
If result = -1 Then
Return False
Else
Return True
End If
End Function

End Class

la idea es simple se le propociona los siguientes datos

seg = New mysqlseguimiento()
seg.ds = ds 'ds es un datase
seg.conecion = "Server=localhost;Database=dbname;Uid=root;Pwd=1234" ' la conecion
seg.disparador = "dis_prueba" ' nombre del disparador
seg.tabla = "prueba" ' nombre de la tabla
seg.campoprimario = "id" 'campo primary key
seg.tipocampo = "INT(11)" 'tipo del campo primary key
seg.iniciseg() ' procedimiento de inicio

luego en Timer._Tick le das el seguimiento a la tabla
seg.SEGIMIENTO()

asi puedes ver los diferentes insert,update y delete que se le den y no castiga el ancho de banda

si nesecitan alguna explicacion me lo hacen llegar por email [email protected]

espero que le sirva a ustedes ...
Datos archivados del Taringa! original
10puntos
1,294visitas
0comentarios
Actividad nueva en Posteamelo
0puntos
1visitas
0comentarios
Dar puntos:

Dejá tu comentario

0/2000

Autor del Post

r
rbam1🇦🇷
Usuario
Puntos0
Posts3
Ver perfil →
PosteameloArchivo Histórico de Taringa! (2004-2017). Preservando la inteligencia colectiva de la internet hispanohablante.

CONTACTO

18 de Septiembre 455, Casilla 52

Chillán, Región de Ñuble, Chile

Solo correo postal

© 2026 Posteamelo.com. No afiliado con Taringa! ni sus sucesores.

Contenido preservado con fines históricos y culturales.