I am learning C #, dedicating myself a lot and I have already been able to create a small system where I consult and insert information in a Mysql database on a linux server. So far so good, but I'd like to avoid having to keep repeating the connection code with the database, or change the connection information like server, database, user, password, port. I would really like to create a txt file containing this information. And that could change depending on the server ip, name of the bank, finally this basic information without having to recompile the whole project. Excerpt of the connection code with mysql database:
private void btnSalvar_Click(object sender, EventArgs e)
{
string constring = "datasource=mysqlip;port=3306;database=winprog;username=root;password=root"; //How to put this part in a .txt file or something that returns the values that it takes for the connection?//
var conexao = new MySqlConnection(constring);
var comando = conexao.CreateCommand();
try
{
conexao.Open();
comando.CommandText = "INSERT INTO name (name,attribute,twoname,valuename) VALUES ('" + nameUser.Text + "','username','=','" + namereal.Text + "')";
comando.ExecuteNonQuery();
}
finally
{
if (conexao.State == ConnectionState.Open)
conexao.Close();
}
}
If I have 10 forms that use code and need to enter data the 10 need to specify the connection information, and if it changes from server everything becomes more complicated. App.config is a bit strange, I would like to simplify this procedure. Thank you all, I apologize for the English.