I'm making a program that can interact with a MySQL database. Everything is working fine, but I need to be able to open a connection named connection or whatever, and use that connection from other places, instead of having to make new connections every time I do something.
Here's my code:
private void button2_Click(object sender, EventArgs e) {
string MyConString = "SERVER=" + ServerTxt.Text + ";" +
"DATABASE=" + DBtxt.Text + ";" +
"UID=" + UIDtxt.Text + ";" +
"PASSWORD=" + Pwdtxt.Text + ";";
SQLconnection = new MySqlConnection(MyConString);
SQLconnection.Open();
}
Which happens when a user fills out the connection info and presses the connect button.
Then in another button which lets the user run queries on a database that has been connected to, this happens:
private void queryBtn_Click(object sender, EventArgs e) {
MySqlCommand command = SQLconnection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = query.Text;
Reader = command.ExecuteReader();
while (Reader.Read()) {
string thisrow = "";
for (int i= 0;i<Reader.FieldCount;i++)
thisrow+=Reader.GetValue(i).ToString() + ",";
listBox1.Items.Add(thisrow);
}
}
But obviously, SQLconnection doesn't exist in the current context. I've tried fiddling with classes and stuff, but as you can see I really don't know what I'm doing, and couldn't figure out the public, static, private, sealed, and all that stuff associated with them. The solution may still lie in classes, but I haven't found it.
I'm using the mysql connector/net, which I have no idea what that means, but it may help you.
Thanks for anything you guys can point out.

New Topic/Question
Reply




MultiQuote





|