I have the form:
Thedatabaseandtheregistrationtablehavealreadybeencreated,IamusingwampserverandMySQLWorkbench.
Myquestioniswheretoputtheconnectionstring:
MySqlConnectionconn=newMySqlConnection("Persist Security Info=False;Server=localhost;Database=banco_teste;uid=root;pwd=;");
Currently my code looks like this:
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 teste_DB
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MySqlConnection conn = new MySqlConnection("Persist Security Info=False;Server=localhost;Database=banco_teste;uid=root;pwd=;");
}
private void btnGravar_Click(object sender, EventArgs e)
{
}
}
}
When I try to open the connection inside the btnGravar_Click
method:
private void btnGravar_Click(object sender, EventArgs e)
{
conn.Open();
}
Visual Studio returns me an error stating that the name conn
does not exist in context.
Where can I declare the connection string so that I can open the connection whenever I want?