How to Form a Correct MySQL Connection String

How to form a correct MySQL connection string?

try creating connection string this way:

MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
conn_string.Server = "mysql7.000webhost.com";
conn_string.UserID = "a455555_test";
conn_string.Password = "a455555_me";
conn_string.Database = "xxxxxxxx";

using (MySqlConnection conn = new MySqlConnection(conn_string.ToString()))
using (MySqlCommand cmd = conn.CreateCommand())
{ //watch out for this SQL injection vulnerability below
cmd.CommandText = string.Format("INSERT Test (lat, long) VALUES ({0},{1})",
OSGconv.deciLat, OSGconv.deciLon);
conn.Open();
cmd.ExecuteNonQuery();
}

How to generate the right connection string for MySql

This could be useful

<connectionStrings>
<add name="MyContext" providerName="MySql.Data.MySqlClient"
connectionString="server=localhost;port=3306;database=mydbname;uid=my_user_id;password=my_password"/>
</connectionStrings>

tipically the my_user_id = root

https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html

MySQL Connection String C#

You are attempting to connect to your MySQL database from your .net code using ODBC. Your error message is telling you that you haven't created an appropriately named ODBC data source object (DSN). You can do that with the ODBC Data Source Adminstrator control panel if you need to.

If I were you I would use Connector/NET instead of ODBC. It performs better and it isn't quite such a pain in the neck to configure correctly.

You can download the install kit for it here. http://dev.mysql.com/downloads/connector/net/

You'll need to change your code for this. But, it's worth it! Seriously! Your code will end up looking like this.

using System;
//etc etc
using MySql.Data.MySqlClient;
//etc etc

namespace myapp
{
class Myclass
{
static void Mymethod(string[] args)
{
string connStr = "server=server;user=user;database=db;password=*****;";
MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();

string sql = "SELECT this FROM that";
MySqlCommand cmd = new MySqlCommand(sql, conn);
using (MySqlDataReader rdr = cmd.ExecuteReader()) {
while (rdr.Read()) {
/* iterate once per row */
}
}
}
}
}

How to give default connection string for mysql database in app.config using c#

If you want to install a MySql Server in user machine, you will have to specify a User and Password as root for the server. Just then use the connection string:

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

Where User is the Username you specified, Password is the Password you specified, and Db is your DataBase. The IP 127.0.0.1 points to the local machine (localhost).

It's important to take care of some things: If you intend to install in a lot of machines, and some of that machines has a chance to already have a server installed, you will need to handle this, just google about.

Also, you may need to perform a silent installation. Here are some examples about how to do it:

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

After that, You can set your application to use this installation using the approach I exposed in my previous answer.

Note: I'll recommend you to use another kind of server. Since we are talking about Windows, why not use MS SQL Server express? It's free, fast and lighter. Also, I bet it'll be a lot more easier to integrate with your application:

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

If you don't need more than 10GB of data, I strongly recommend you to use MSSQL express!


Previous Answer:

In App.config:

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

In C# (I guess you are using the Mysql Connector library):

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

Just remember that exposing the connection string of Your DataBase in Client Machines is a extremely security risk!

What are examples of how to code a MySql Connection string?

I would use the full domain name of the server. The IP should work too. You may need to include the port. Port 3306 is the default MySql port. This information should be provided by the hosting service. Most likely found in the remote admin tool provided by the web host.

here are other resources to:

https://www.connectionstrings.com/mysql/

How to connect to MySQL Database?

example:
http://www.codeproject.com/Articles/71346/Connecting-to-MySQL-Database-using-C-and-NET

Connection string to connect to MySql database?

It will probably want to know the password ;) Your message box says so:

using password: NO... Access Denied...

Like so:

server=localhost;uid=root;pwd=YOURPASSWORDHERE;database=YOURDATABASENAMEHERE;

Specifying password in MySQL connection string

Password and DB name also a part of connection string.

Ex: mysql://root:password@localhost:port/dbName



Related Topics



Leave a reply



Submit