Author Topic: Insert into MySQL  (Read 3725 times)

aezrath

  • Newbie
  • *
  • Posts: 4
Insert into MySQL
« on: March 26, 2022, 03:08:10 PM »
So I googled for this as my familiarity is only with MySQL (nothing else in regards to databases) and i couldn't find it anywhere except in one plugin that I think is  SQLite and I have no idea how to use it.

As I play bit with PHP and Node and stuff, I use PHPMyAdmin a lot, so I made this (with a lot of google's help) as the only solution that has actually worked for me:

C#

Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

public class VAInline
{
    public void main()
    {

      // This is my connection string i have assigned the database file address path 
      string connetionString = null;
      // Assign database details
      connetionString = "server=localhost;database=dbname;uid=username;pwd=password;";

      using (MySqlConnection cn = new MySqlConnection(connetionString))
        {
        try
          {
          // change the table and values, divide values by comma
          string query = "INSERT INTO table(Value) VALUES (?value);";
          cn.Open();
          using (MySqlCommand cmd = new MySqlCommand(query, cn))
          {
            // change value, type from "VarChar" to your input type and add your variable. For multiple, just copy/paste and edit parameters
            cmd.Parameters.Add("?value", MySqlDbType.VarChar).Value = VA.GetText("textvariable");
            cmd.ExecuteNonQuery();
          }
      }
          catch (MySqlException ex)
      {
          MessageBox.Show("Error in adding mysql row. Error: "+ex.Message);
      }
    }
  }
}

And i am including the .dll file below.

As it is the first inline coming from me, there could be improvement and writing to VA log but this is just basic functionality.

License for the .dll:
https://downloads.mysql.com/docs/licenses/connector-net-8.0-gpl-en.pdf
« Last Edit: March 26, 2022, 10:13:22 PM by aezrath »