Click here to Skip to main content
12,451,251 members (52,122 online)
Click here to Skip to main content
Articles » Database » Database » MySQL » Revisions
Add your own
alternative version

Tagged as


11 bookmarked

C# ASP.NET MySQL Connection Tutorial with MySQL Connector

, 28 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
MySQL connection tutorial with MySQL connector.

In order to connect to MySQL Server with .NET in C# or ASP.NET:

  • You need to download MySQL Connector/Net .
  • After you add a reference to your project, it is probably in C:\Program Files\MySQL\MySQL Connector Net 5.0.7\Binaries\.NET 2.0 folder, add the MySql.Data.dll file as a reference.
  • Make your connection string, the following code will shows a standard MySQL connection string.
  • using MySql.Data.MySqlClient;
    public static string GetConnectionString()
      string connStr = String.Format("server={0};user id={1}; password={2};" + 
        "database=yourdb; pooling=false", "yourserver",
        "youruser", "yourpass");
      return connStr;
  • Then create an instance from MySql.Data.MySqlClient.MySqlConnection as shown below.
  • MySql.Data.MySqlClient.MySqlConnection mycon = 
      new MySqlConnection( GetConnectionString());
  • Then try to open the MySqlConnection.
  • if(mycon .State != ConnectionState.Open)
      mycon .Open();
    catch (MySqlException ex)
      throw (ex);

So simple as you see, It is always better to do this in data access layer also called DAL.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Yaseer Arafat
Software Developer (Senior)
United States United States
• Experience in analysis, design and development of client/server, web based and n- tier application. Expert in developing windows applications, web applications, windows services and web services in Microsoft Visual Studio .Net IDE using C#.Net 1.1, C#.Net 2.0, VB.NET, ASP.NET, ADO.NET and SQL Server.
• Proficient in Web Services, SOAP, XML, XSL, XSLT, Xpath, XSL-FO, XPointer, DTD, XML Schema, CSS, DOM, HTML, AJAX and JavaScript, JQuery.
• Created deployment packages for Applications using Visual studio.Net Startup Project, which involves creating native image of an assembly, installing an assembly in Global assembly cache (GAC)
• Worked extensively with Data Adapter, Dataset, Data reader as a part of ADO.NET to access and Update database.
• Extensive experience in SQL Server Database design, Database maintenance, developing T-SQL queries, stored procedures, and triggers using SQL Server 2000/2005/2008.
• Good working knowledge with designer tools such as Microsoft Visio.
• Good working knowledge in designing Use Case, Class, Sequence, Collaboration, State,
Component, Deployment, Activity diagrams using UML
• Expertise in designing UML design patterns such as Adapter, Proxy, Bridge, and Facade and so on
• Expert in dealing with Private, Shared satellite and resource assemblies, configuring them with strong names, Deploying in GAC
• Expert in debugging an application using the debugging tools provided by the Visual Studio.Net IDE.
• Functional domain experience involves Financial, Telecom and Hospital management.
• Effective in working independently and collaboratively in teams.

Specialties:C#.NET,ASP.NET,ASP.NET MVC3,EnityFramework,JQuery,Linq to SQL,Oracle11g,TSQL,HTML,CSS,DevExpress,Crystal Reports.

Linkedin Profile

You may also be interested in...


Comments and Discussions

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 28 Mar 2012
Article Copyright 2012 by Yaseer Arafat
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid