[VB.NET] MySQL Function to Insert/update/delete with parameters

  • Hello Guest! Welcome to ConsoleCrunch, to help support our site check out the premium upgrades HERE! to get exclusive access to our hidden content.
  • Unable to load portions of the website...
    If you use an ad blocker addon, you should disable it because it interferes with several elements of the site and blocks more than just adverts.
  • Read Rules Before Posting Post Virus Scans with every program.

1UP

Member
Oct 9, 2014
24
10
63
I wrote this bit of code in a project that I am working on for taking screenshots and uploading them to a remote server.

Basically what this does is it accepts the following parameters:
SQLQuery as a string
Parameters and Values as a string array

I am sure there are probably better ways of doing this however this got the job done in a very little amount of code.

In order to use this you would need to include the MySql dll into your project and then refer to it.

Code:
 Imports MySql.Data.MySqlClient
Code:
  Dim connString As String = "server=server.com;database=db_Name;port=3306;user=db_user;password=db_password"

Code:
  Public Sub SaveUpdateDelete(ByVal sql As String, ByVal parameters() As String, ByVal Values() As String)
  Dim con As MySqlConnection = New MySqlConnection(connString)

  con.Open()
  Dim cmd As MySqlCommand = New MySqlCommand(sql, con)

  For i = 0 To parameters.Count - 1
  cmd.Parameters.AddWithValue("@" & parameters(i).ToString, Values(i))
  Next
  cmd.CommandText = sql
  cmd.ExecuteNonQuery()

  con.Close()
  End Sub

To make use of this function:
Code:
Private sub DoDBStuff
 Dim sql As String = "INSERT INTO db_table(userID,imageHash,notes,lastEditedDate) VALUES(@userID,@imageHash,@imageDescription,NOW())"
Dim params() As String = {"userID", "imageHash", "imageDescription"}
Dim Values() As String = {DataBase_UserID, DataBase_UserName & "/" & foldername & "/" & HashedFileName & "." & FileNameParts(FileNameParts.Count - 1).ToString, ""}

SaveUpdateDelete(sql, params, Values)
End Sub
 
General chit-chat
Help Users
  • Chat Bot:
    sc88tuyensinhnganhan is our newest member. Welcome!
  • Chat Bot:
    Christo has joined the room.
  • Chat Bot:
    phantomxx2002 is our newest member. Welcome!
  • Chat Bot:
    8xbetccorg is our newest member. Welcome!
  • Chat Bot:
    sc88link2 is our newest member. Welcome!
  • Chat Bot:
  • Chat Bot:
    wdlkmawlkdawkjndawd is our newest member. Welcome!
  • Chat Bot:
    QM|T has joined the room.
  • Chat Bot:
    phimmoi888com is our newest member. Welcome!
  • Chat Bot:
    sc88adcom3 is our newest member. Welcome!
  • Chat Bot:
    ramirezthegoat is our newest member. Welcome!
  • Chat Bot:
    dddphcomcom2 is our newest member. Welcome!
  • Chat Bot:
    zzj88com2 is our newest member. Welcome!
  • Chat Bot:
    Blanche James is our newest member. Welcome!
  • Chat Bot:
    nohudynamicmodels is our newest member. Welcome!
  • Chat Bot:
    QM|T has joined the room.
  • Chat Bot:
    kqxsonline is our newest member. Welcome!
  • Chat Bot:
    dariob2001 is our newest member. Welcome!
  • Chat Bot:
    luck8red1 is our newest member. Welcome!
  • Chat Bot:
    sassan is our newest member. Welcome!
  • Chat Bot:
    karsan is our newest member. Welcome!
  • Chat Bot:
    79kingnews is our newest member. Welcome!
  • Chat Bot:
    wwwdddphcom is our newest member. Welcome!
  • Chat Bot:
    QM|T has joined the room.
      Chat Bot: QM|T has joined the room.