0

Actually i'm formatting data to insert in my MySQL DB and i'm at the point where i'm managing the NULL values.

So for manage it i've set a try catch inside the if where i look for datatype and now i have to put in the Catch exception the default value of certain type.

But how can i do it in VB.NET?

Here is my code, any suggestion for code improvment will be grateful

  For Each row As DataRow In ds.Tables(0).Rows
            righe = ""
            For Each column As DataColumn In ds.Tables(0).Columns
                If column.DataType = Type.GetType("System.DateTime") Then
                    Try
                        righe &= "'" & Format(row.Item(column.ColumnName), "yyyy-MM-dd") & "', "
                    Catch ex As Exception

                    End Try

                ElseIf column.DataType = Type.GetType("System.TimeSpan") Then
                    righe &= "'" & Format(row.Item(column.ColumnName).ToString, "HH\:mm\:ss") & "', "
                ElseIf column.DataType = Type.GetType("System.Int32") Then
                    righe &= row.Item(column.ColumnName) & ", "
                ElseIf column.DataType = Type.GetType("System.Single") Then
                    righe &= Replace(row.Item(column.ColumnName), ",", ".") & ", "
                ElseIf column.DataType = Type.GetType("System.Byte") Then
                    righe &= row.Item(column.ColumnName) & ", "
                ElseIf column.DataType = Type.GetType("System.Double") Then
                    righe &= Replace(row.Item(column.ColumnName), ",", ".") & ", "
                ElseIf column.DataType = Type.GetType("System.String") Then
                    righe &= "'" & APICI(row.Item(column.ColumnName)) & "', "
                End If
            Next
            righe = Mid(righe, 1, Len(righe) - 2)
        Next
NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100
  • check out parameterized insert so you dont have to build strings. https://stackoverflow.com/questions/16167924/c-sharp-with-mysql-insert-parameters – Jeremy Feb 28 '19 at 18:20
  • also, if you want to avoid default values, define the default value in the DB, and if the value you detect is blank, then simply don't insert that column, and the DB will put in whatever default value you want. – Jeremy Feb 28 '19 at 18:42
  • @Jeremy actually i've chose not to parametrize the insert as the tables are different with different columns. While i can't set the DB default value as i'd work at a yet created DB from another developer – NiceToMytyuk Mar 01 '19 at 13:37

0 Answers0