C#/MySql 2: Connection
Written by:
maffelu
, 2009-04-17 07:27:12
Database 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.
Data Source
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.
Port
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.
Database
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
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
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
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.
Other attributes
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.
Example:
string ConnectionString = "Data source=" txtConHost.Text ";Database="
txtConDB.Text ";UserId=" txtConUser.Text
";Password=" txtConPass.Text;
or
string ConnectionString = "Host=localhost;Database=MyDB;Persist security info=true;Uid=bob;PWD=hope;";
And instantiate MySqlConnection class:
MySqlConnection Connect;
Connect = new MySqlConnection(ConnectionString);