Im new to C# and I just create a database connection using mysql. Then I need to define a database connection class here this is my code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MySql.Data.MySqlClient; namespace restsurant_pos { class mysqlDbConnect { public static MySqlConnection GetConnection(){ string MyConnectionString = "Server=Localhost;Database=pos;Uid=root;Pwd='';"; MySqlConnection connection = new MySqlConnection(MyConnectionString); MySqlCommand cmd; connection.Open(); return connection; } hope it is correct..
then how can I create a object using this class. I just created a as below, but it is getting an error.
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; namespace restsurant_pos { public partial class AddCategory : Form { public AddCategory() { InitializeComponent(); } private void btn_insertCategory_Click(object sender, EventArgs e) { string categoryName = txtCategoryName.Text; mysqlDbConnect connection = new mysqlDbConnect(); try { cmd = connection.CreateCommand(); cmd.CommandText = "INSERT INTO categories(name) VALUES(@categoryName)"; cmd.Parameters.AddWithValue("categoryName", txtCategoryName.Text); cmd.ExecuteNonQuery(); this.Close(); } catch (Exception) { throw; } finally { if (connection.State == ConnectionState.Open) { connection.Close(); } } } } } what is the wrong with my code.. pls help


cmd. Try puttingvarorMySqlConnectionin front of it