1
I have the form:
Already created the database and table for registration, I am using wampserver and Mysql Workbench.
My question is where to place the connection string:
MySqlConnection conn = new MySqlConnection("Persist Security Info=False;Server=localhost;Database=banco_teste;uid=root;pwd=;");
Currently my code is 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 within the method btnGravar_Click
:
private void btnGravar_Click(object sender, EventArgs e)
{
conn.Open();
}
Visual Studio returns me error stating that the name conn
does not exist in the context.
Where I can declare the connection string so I can open the connection whenever I want?