Torna al Thread

Public Function SincronizzaDb(ByVal DT As DataTable, ByVal DTMysql As DataTable, ByVal DataultimaSincrinizzazione As String, ByVal NomeTabella As String, ByVal conn1 As OleDbConnection, ByVal MysqlConn As MySqlConnection) As String ' Dim NomeTabella As String = DT.ToString Dim Col As Int16 Dim i As Integer Dim strsqlInsert As String = " INSERT INTO " & NomeTabella & " (" Dim StrsqlUpdate As String = " UPDATE " & NomeTabella & " SET " Dim StrValues As String = " VALUES (" Dim NomeColonna As String Dim IndiceAutoIncremet As Int16 '----cerco il contatore (da modificare per multichiavi)----------------- 'For Col = 0 To DT.Columns.Count - 1 ' If DT.Columns(i).AutoIncrement = True Then ' IndiceAutoIncremet = i ' End If ' Next IndiceAutoIncremet = 1 'LO METTO AD UNO IN QUANTO devo cercare il modo di reperire il campo chiave '--------stringa sql INSERT------ For Col = 0 To DT.Columns.Count - 1 ' If Col <> IndiceAutoIncremet Then strsqlInsert = strsqlInsert & " " & DT.Columns(Col).Caption & "," StrValues = StrValues & "@" & DT.Columns(Col).ColumnName() & "," '? ' End If Next strsqlInsert = Mid(strsqlInsert, 1, Len(strsqlInsert) - 1) 'tolgo l'ultima virgola StrValues = Mid(StrValues, 1, Len(StrValues) - 1) strsqlInsert = strsqlInsert & " )" strsqlInsert = strsqlInsert & StrValues & ")" '--------stringa sql UPDATE------ For Col = 0 To DT.Columns.Count - 1 If Col <> IndiceAutoIncremet Then StrsqlUpdate = StrsqlUpdate & " " & DT.Columns(Col).Caption & "=?," End If Next StrsqlUpdate = Mid(StrsqlUpdate, 1, Len(StrsqlUpdate) - 1) 'tolgo l'ultima virgola '--------parametri---------- Dim CMDIns As New MySqlCommand(strsqlInsert, MysqlConn) Dim CMDUPd As New MySqlCommand(StrsqlUpdate, MysqlConn) For Col = 0 To DT.Columns.Count - 1 NomeColonna = "?" ' DT.Columns(i).Caption ' Dim param As New OleDbParameter(DT.Columns(y).ColumnName(), GetOleDbType(DT.Columns(y).DataType())) CMDIns.Parameters.Add("@" & DT.Columns(Col).ColumnName(), GetOleDbType(DT.Columns(Col).DataType())) '---l'update non vuole il contatore------------------ If Col <> IndiceAutoIncremet Then NomeColonna = "?" ' DT.Columns(i).Caption ' Dim param As New OleDbParameter(DT.Columns(y).ColumnName(), GetOleDbType(DT.Columns(y).DataType())) CMDUPd.Parameters.Add(NomeColonna, GetOleDbType(DT.Columns(Col).DataType())) End If Next '-------valori con insert DT---------- For i = 0 To DT.Rows.Count - 1 If RecordEsiste(DTMysql, DT.Rows(i)(IndiceAutoIncremet), DT.Columns(IndiceAutoIncremet).Caption) = True Then For Col = 0 To DT.Columns.Count - 1 If Col <> IndiceAutoIncremet Then NomeColonna = DT.Columns(Col).Caption CMDUPd.Parameters("?").Value = DT.Rows(i)(NomeColonna) 'NomeColonna End If Next CMDUPd.CommandText = StrsqlUpdate & " WHERE " & DT.Columns(IndiceAutoIncremet).Caption & "=" & DT.Rows(i)(IndiceAutoIncremet) CMDUPd.ExecuteNonQuery() Else For Col = 0 To DT.Columns.Count - 1 NomeColonna = DT.Columns(Col).ColumnName() 'DT.Columns(Col).Caption CMDIns.Parameters("@" & NomeColonna).Value = DT.Rows(i)(NomeColonna) Next CMDIns.ExecuteNonQuery() End If Next End Function Private Function RecordEsiste(ByVal DT As DataTable, ByVal Codice As String, ByVal NomeCampo As String) As Boolean Dim i As Integer For i = 0 To DT.Rows.Count - 1 If DT.Rows(i)(NomeCampo) = Codice Then Return True End If Next Return False End Function Private Function GetOleDbType(ByVal sysType As Type) As MySqlDbType If sysType Is GetType(String) Then Return MySqlDbType.VarChar ElseIf sysType Is GetType(Integer) Then Return MySqlDbType.Int16 ElseIf sysType Is GetType(Boolean) Then Return MySqlDbType.Bit ElseIf sysType Is GetType(Date) Then Return MySqlDbType.Date ElseIf sysType Is GetType(Char) Then Return MySqlDbType.String ElseIf sysType Is GetType(Decimal) Then Return MySqlDbType.Decimal ElseIf sysType Is GetType(Double) Then Return MySqlDbType.Double ElseIf sysType Is GetType(Single) Then Return MySqlDbType.Int64 ElseIf sysType Is GetType(Byte()) Then Return MySqlDbType.Byte ElseIf sysType Is GetType(Guid) Then Return MySqlDbType.VarChar End If End Function
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5