C#/MySql 2: Connection
A Connection to your database will probably be the most important code in your project since nothing will work without it
With myODBC it looks like this:
string ConnectionString = "Data source=xxx;Port=xxx;Database=xxx;UserId=xxx;Password=xxx;";
You don't have to set the driver since it will be obvious that you're using MySql. The reason it will be so obvious is because you're using the MySqlConnection class which can be found in the MySql.Data.MySqlClient namespace. The constructor of the MySqlConnection class will take either nothing or a connection string as an argument.
Let's go through the connection string a bit closer though.
There are several attribute names for the data source, I use Data Source cause it's obvious. Other equal attributes are DataSource, Host, Server, Addr, Address and Network Address.*
The value of the datasource is the information regarding where to look for the data. If you have the server on your own computer then the data source should be either localhost or 192.168.0.1, otherwize it could be a URL or a IP to the database server.
This is not so important if you have the server on the same computer as the application, but otherwize this is where to set the port settings.
This is the name of the database to connect to. This is not necessary to include if you don't want to connect to a specific database at connection.
There are several attribute names for the database. I use Database cause it's obvious. Other equal attributes are Initial Catalog.*
UserId is the user information to the database login. Comes with a Password
There are several attribute names for the UserId. I use UserId cause it's obvious. Other equal attributes are User Id, UID, User Name and Username.*
Password is the password information to the database login. Comes with a UserId
There are several attribute names for the Password. I use Password cause it's obvious. Other equal attributes are PWD.*
Security is always nice to have and can be vital even if you're making a small local application that only works within the base environment of your computer.
The attribute Persist Security Info gives you the possibility to control wether or not a user can aquire the connection string with login information. The values can be true, false, yes or no. If yes or true then if the user requests the connection string it will be shown with the login information, if false or no, the login information will not be shown.
Some attributes that could be included:
Encrypt, Connection Timeout (or Connect Timeout), CharSet (or Character Set), Port, Protocol, Logging, Allow Batch, Shared Memory Name, Allow Zero Datetime, Old Syntax (or OldSyntax), Connection Lifetime, Max Pool Size, Min Pool Size, Pooling, and Pipe Name (or Pipe).
* All attributes in the connection string are non-case sensitive meaning UID is the same as Uid or uiD.
//From a form
string ConnectionString = "Data source=" txtConHost.Text ";Database="
txtConDB.Text ";UserId=" txtConUser.Text
//With default values
string ConnectionString = "Host=localhost;Database=MyDB;Persist security info=true;Uid=bob;PWD=hope;";
And instantiate MySqlConnection class:
//Set the MySqlConnection to your commandline string
Connect = new MySqlConnection(ConnectionString);
Feel free to comment this article using a facebook profile.
I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.