如何使用c#为app.config中的mysql数据库提供默认连接字符串

时间:2015-01-23 06:16:54

标签: c# mysql .net ado.net connection-string

如何在app.config中为MySQL数据库提供默认连接字符串,这样当我在其他系统中安装MySQL时,必须接受默认连接字符串。例如对于sql server,我们使用数据源:作为多个系统的默认连接。我需要在app.config中使用相同的MySQL连接字符串

1 个答案:

答案 0 :(得分:0)

如果要在用户计算机中安装MySql Server,则必须以服务器的root身份指定用户和密码。然后使用连接字符串:

" server = 127.0.0.1; user id = User ; password = Password ; database = Db "

其中用户是您指定的用户名,密码是您指定的密码, Db 是您的数据库。 IP 127.0.0.1指向本地计算机(localhost)。

照顾好一些事情很重要:如果你打算在很多机器上安装,而且有些机器有机会安装服务器,你需要处理这个问题, google about。

此外,您可能需要执行静默安装。以下是有关如何执行此操作的一些示例:

http://hamidseta.blogspot.com.br/2008/05/install-mysql-server-50-silently.html http://forums.mysql.com/read.php?11,26486,42152#msg-42152

  

mysqlmonitor-version-windows-installer.exe --optionfile options.server.txt

http://forums.mysql.com/read.php?11,26486,42152#msg-42152 http://dev.mysql.com/doc/refman/5.1/en/mysql-config-wizard-cmdline.html

之后,您可以使用我在上一个答案中公开的方法将您的应用程序设置为使用此安装。

注意:我建议您使用其他类型的服务器。既然我们在谈论Windows,为什么不使用MS SQL Server express?它是免费的,快速的,更轻的。此外,我敢打赌,它可以更容易地与您的应用程序集成:

http://blog.sqlauthority.com/2009/08/26/sql-server-sql-server-express-a-complete-reference-guide/ Can I deploy SQL Server Express with my desktop application just like builtin database? http://www.microsoft.com/web/platform/database.aspx http://www.microsoft.com/en-us/server-cloud/products/sql-server-editions/sql-server-express.aspx

如果您不需要超过10GB的数据,我强烈建议您使用MSSQL Express!


上一个答案:

在App.config中:

<configuration>
    <appSettings>
        <add key ="MySqlConnectionString" value = "server=*Server.com*;user id=*User*;password=*Password*;database=*Db*;persist security info=True;"/>
    </appSettings>
</configuration>

在C#中(我猜你使用的是Mysql Connector库):

var objConnection = New MySqlConnection();
objConnection.ConnectionString = ConfigurationManager.AppSettings["MySqlConnectionString"];

请记住,在客户端计算机中公开数据库的连接字符串存在极大的安全风险!