title: VB.net save data codes
body: <p>I am new to vb.net and Mysql, and I came across some codes all over the internet on how to connect and save vb data on mysql database..</p>
  
  <p>This is the first type of codes that I encountered in saving data..</p>
  
  <pre><code>Sub saveData()
      Dim mysqlConn As MySqlConnection
      Dim cmDsql As MySqlCommand
      mysqlConn = New MySqlConnection
      mysqlConn.ConnectionString =   "server=localhost;user=root;password=root;database=dbsample"
      Dim rdr As MySqlDataReader
      Try
          mysqlConn.Open()
          Dim sqlQuery As String
          sqlQuery = "INSERT INTO dbsample.tblsample (clName,clAge,clAddress) values ('" &amp; txtName.Text &amp; "','" &amp; txtAge.Text &amp; "','" &amp; txtAddress.Text &amp; "')"
          cmDsql = New MySqlCommand(sqlQuery, mysqlConn)
          rdr = cmDsql.ExecuteReader
          MessageBox.Show("Data Saved")
          txtName.Clear()
          txtAge.Clear()
          txtAddress.Clear()
          mysqlConn.Close()
      Catch ex As Exception
          MessageBox.Show(ex.Message)
      Finally
          mysqlConn.Dispose()
      End Try
  End Sub     
  </code></pre>
  
  <p>This is the second one...</p>
  
  <pre><code>Sub saveData2()
      Dim mysqlconn As New MySqlConnection
      mysqlconn = New MySqlConnection
      mysqlconn.ConnectionString = "server=localhost;user=root;password=root;database=dbsample"
      Dim cmdsql As MySqlCommand
      Dim sqlQuery As String
      Dim fName As String = txtName.Text
      Dim fAge As Integer = txtAge.Text
      Dim fAdd As String = txtAddress.Text
      sqlQuery = "INSERT INTO dbsample.tblsample (clName,clAge,clAddress) VALUES (@clName,@clAge,@clAddress)"
      cmdsql = New MySqlCommand(sqlQuery, mysqlconn)  
      Try
          mysqlconn.Open()
          With cmdsql.CommandText = sqlQuery
              cmdsql.Connection = mysqlconn
              cmdsql.Parameters.AddWithValue("@clName", fName)
              cmdsql.Parameters.AddWithValue("@clAge", fAge)
              cmdsql.Parameters.AddWithValue("@clAddress", fAge)
              cmdsql.ExecuteNonQuery()
              MessageBox.Show("Data Saved")
              txtName.Clear()
              txtAge.Clear()
              txtAddress.Clear()
          End With
      Catch ex As Exception
          MessageBox.Show(ex.Message)
      Finally
          mysqlconn.Close()
      End Try
  End Sub
  </code></pre>
  
  <p>These two codes both saves data in mysql database successfully... I just want to know what is the difference between these and what does their format in connecting to database means, can someone please explain these two codes for me.. Please forgive my ignorance :) Thanks in advance...</p>