MySql Insert with Parameters (as explicit as posible)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: MySql Insert with Parameters (as explicit as posible)

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Posts
    245

    MySql Insert with Parameters (as explicit as posible)

    I used this method in previous versions I'm not sure if it was the old connector i used by then but now it throws "Input string was not in a correct format." at the line "sql.ExecuteNonQuery()"

    Using VB.net 2013 express

    what am i doing wrong or where can i find a good example to insert using parameters but i read somewhere that the method with "addwithvalue" uses implicit conversions and i always target for the best practices i dont want implicit conversions kicking me in the future.

    Thx in advance.

    Code:
     
        Public Function AddCliente(ByRef Cedula As String, ByRef Nombres As String, ByRef Apellidos As String, ByRef Telefono As String, ByRef Movil As String, ByRef Direccion As String) As Boolean
            Dim DBCon As MySqlConnection
            DBCon = New MySqlConnection(ConnStr)
            Try
                'Conexion
                DBCon.Open()
                '........
                Dim sql As MySqlCommand = New MySqlCommand
                sql.Connection = DBCon
                'Formateando fechas para almacenarlas en la BD
                sql.CommandText = "INSERT INTO Clientes VALUES(@Par1,@Par2,@Par3,@Par4,@Par5,@Par6)"
    
                Dim Par1 As New MySqlParameter("@Par1", DbType.String)
                Dim Par2 As New MySqlParameter("@Par2", DbType.String)
                Dim Par3 As New MySqlParameter("@Par3", DbType.String)
                Dim Par4 As New MySqlParameter("@Par4", DbType.String)
                Dim Par5 As New MySqlParameter("@Par5", DbType.String)
                Dim Par6 As New MySqlParameter("@Par6", DbType.String)
    
                Par1.Value = Cedula
                Par2.Value = Nombres
                Par3.Value = Apellidos
                Par4.Value = Telefono
                Par5.Value = Movil
                Par6.Value = Direccion
    
    
                sql.Parameters.Add(Par1)
                sql.Parameters.Add(Par2)
                sql.Parameters.Add(Par3)
                sql.Parameters.Add(Par4)
                sql.Parameters.Add(Par5)
                sql.Parameters.Add(Par6)
    
    
                sql.CommandType = CommandType.Text
                sql.ExecuteNonQuery()
                'Cerramos
                DBCon.Close()
                Return True
                '---------
            Catch ex As MySqlException
                DBCon.Close()
                'MsgBox(ex.Message)
                If (ex.Number) = 1045 Then
                    MsgBox("No se pudo realizar la conexion con el servidor sql. porfavor revisar las configuraciones IP/Puerto del Servidor o de que este se encuentre activo", MsgBoxStyle.Information, "Aviso")
                End If
                Return False
            Catch ex As Exception
                DBCon.Close()
                MsgBox(ex.Message, MsgBoxStyle.Information, "AddCliente")
                Return False
            End Try
        End Function

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: MySql Insert with Parameters (as explicit as posible)

    MySQL export:

    Code:
    INSERTINTO `customers` (`CustomerId`
     `CustomerLast`
     `CustomerFirst`
     `CustomerCell`
     `CustomerHome`
     `CustomerWork`
     `CustomerEmail`
    \\ 50 more fields
     `CustomerLastType`
     `CustomerLastLocation`) VALUES 
    (\\ 50 more fields)
    just finished importing 85K rows of 100 fields
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center