Home of the AlmostImplementedException

How to connect to database

Today we provide a snippet for prospective developers. In fact how to easily get a connection to a database.
I use a method which can be changed easily to connect to other database, e.g. Acces and MySql. In my example we will refer to Ms Sql.
In the example code below you can see that I use System.Data.SqlClient.SqlConnection and System.Data.SqlClient.SqlCommand for instantiation.
For a connection to an access database you would need System.Data.Odbc.OdbcConnection and System.Data.Odbc.OdbcCommand. Furthermore you have to specify the database driver which must be installed at the system. I think for Access 2003 it was:

When you want connect to a MySql database you will need the MySql-Connector for .Net. You can find it at the page linked below.

The instantiation for the connection is provided by MySql.Data.MySqlClient.MySqlConnection and MySql.Data.MySqlClient.MySqlCommand.

Share :

Comments are currently closed.