Click here to Skip to main content
Click here to Skip to main content

Tagged as

C# ASP.NET MySQL Connection Tutorial with MySQL Connector

, 28 Mar 2012
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)
    try
    {
      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.

License

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

Share

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
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 1 Pinmemberaryaverman1888-Aug-13 1:45 
GeneralRe: My vote of 1 PinmemberYaseer Arafat18-Aug-13 12:02 
QuestionDriver PinmemberMohamad777-Aug-13 1:03 
AnswerRe: Driver PinmemberYaseer Arafat18-Aug-13 12:01 
QuestionThanks PinmemberYaseer Arafat8-Mar-13 6:47 
Thanks for being with the Article.I am going to provide full solution with in a week.
Thanks
Yaseer Arafat
Senior Software Engineer
Wooskie Inc
email:mike@wooskie.com
Web:www.wooskie.com

Generalhttp://dotnetnukes.blogspot.in/ Pinmemberchitranjan singh rathore1-Feb-13 1:00 
General<a href="http://dotnetnukes.blogspot.in/<code></code>"></a> Pinmemberchitranjan singh rathore1-Feb-13 0:56 
QuestionLogin page PinmemberSandunika Nilmadi9-Dec-12 19:10 
AnswerRe: Login page PinmemberYaseer Arafat8-Mar-13 6:40 
SuggestionAdd source code please Pinmemberragunaat919-Jul-12 3:36 
GeneralRe: Add source code please PinmemberSrinivas_rocks16-Feb-14 23:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 28 Mar 2012
Article Copyright 2012 by Yaseer Arafat
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid