txt文件配置中的C#Mysql连接 [英] C# Mysql connection in txt file config

查看:109
本文介绍了txt文件配置中的C#Mysql连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C#,并投入了大量心血,而且我已经能够创建一个小型系统,在其中我可以在Linux服务器上的Mysql数据库中查询和插入信息. 到目前为止,一切都很好,但我想避免不得不继续与数据库重复连接代码,或更改连接信息,例如服务器,数据库,用户,密码,端口. 我真的很想创建一个包含此信息的txt文件. 而这可能会根据服务器ip,银行名称以及最终的基本信息而有所变化,而不必重新编译整个项目. 摘录与mysql数据库的连接代码:

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();

        }

    }

如果我有10个使用代码的表格并需要输入数据,则这10个表格需要指定连接信息,并且如果服务器中的连接信息发生变化,一切将变得更加复杂. App.config有点奇怪,我想简化此过程. 谢谢大家,我对英语表示歉意.

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.

推荐答案

不建议使用文本文件存储连接字符串,但由于您在询问那么,该怎么做呢?

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. 在文件中写入连接字符串.
  2. 将文件移动到可执行路径.例如:

bin/debug文件夹

bin/debug folder

  1. 阅读:

  1. Read it:

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

现在您可以使用connectionString变量.

now you can use the connectionString variable.

但是

正如我提到的那样,不建议这样做,因为您很难理解App.Config的工作原理.

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

在App.Config中,您将创建一个新的Element/Entry.每个条目都有一个名称和一个.

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>

现在,您有一个名为 ConnectionStrings 的元素,该元素可以包含多个/连接字符串列表.我们将使用名称 MyDatabaseCS 添加一个新的连接字符串,并将您的连接字符串作为 .稍后我们将使用名称来获取连接字符串的值.

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.

现在可以在代码中获取连接字符串值.您只需要引用连接字符串元素,由于您可能有很多连接字符串,因此您必须区分您的确切意思,在这种情况下,它就是 MyDatabaseCS

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;

这篇关于txt文件配置中的C#Mysql连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆