2

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.

banana
  • 31
  • 2
  • 10
  • " I would really like to create a txt file containing this information". That would be App.config or Web.config – devlin carnate Jan 09 '18 at 22:10
  • The app.config is the designated file to store that type of information. – hbulens Jan 09 '18 at 22:10
  • How do you find app.config strange or think it’s simpler to do with your own routines reading files? The system will read the config for you and provide the connection string through a standard method. – Sami Kuhmonen Jan 09 '18 at 22:12
  • Possible duplicate of [Where Should I Store a database Connection String?](https://stackoverflow.com/questions/4177060/where-should-i-store-a-database-connection-string) – devlin carnate Jan 09 '18 at 22:20

2 Answers2

4

Use the App.config file and specifically look into the element. In there you define a connection string by name and get it using

ConfigurationManager.ConnectionStrings["<you connection string name>"].ConnectionString;

This this page for a full description How to get a Connection String From App Config in C#

Also look into using configuration transforms to allow redifinition of values in the config which automatically get selected for different target environments.

Stephen York
  • 1,247
  • 1
  • 13
  • 42
-1

It's not recommended to use a text file to store a connection string but since you are asking on how to do it this way then:

  1. Write the connection string in a file.
  2. Move the file to the executable path. ex:

bin/debug folder

  1. Read it:

    var connectionString = File.ReadAllLines("Connectionstring.txt").FirstOrDefault();

now you can use the connectionString variable.

But

as I mentioned that it's not recommended and since you're having hard time understanding how the App.Config works.

In the App.Config you create a new Element/Entry. Every entry has a Name and a Value.

<configuration>
   <connectionStrings>
       <add name="MyDatabaseCS"
        connectionString="datasource=mysqlip;port=3306;database=winprog;username=root;password=root"/>
   </connectionStrings>
</configuration>

Now you've an Element called ConnectionStrings which can have a multiple/list of connection strings. We will add a new connection string with the name MyDatabaseCS and has as a value your connection string. We'll use the Name later to get the Value of the connection string.

Now to get the Connection string value in your code. You'll just have to refer the Connection String Element and since you may have many connection strings you'll have to differentiate which one you exactly mean and in this case it's the MyDatabaseCS

var connectionString = ConfigurationManager.ConnectionStrings["MyDatabaseCS"].ConnectionString;
Community
  • 1
  • 1
shawkyz1
  • 886
  • 5
  • 19