connecting to mysql DB

Posted on 2005-05-04
Last Modified: 2010-04-16
I am very new to C# and programming. I am using Visual trial version. I have a mysql DB for my PHP website. Could some one explain in simple terms how I would connect to this. I went to toolbox and tried oledbdatapter but the only options i was given by the wizard was to microsoft databases ie jet etc....
any hints on querying would be good as well. cheers.
Question by:jdav3579
    LVL 35

    Expert Comment

    In the web.config file for your website

                  <add key="connString" value="server=localhost;Uid=Username;Password=YourPassword;Database=YourDB" />

    Then a simple page

    <%@ Page Language="C#" ContentType="text/html" debug="true"%>
    <%@ Import Namespace="MySql.Data.MySqlClient" %>
    <%@ Import Namespace="System.Data" %>

    <script language="c#" runat="server">

    void Page_Load()
          MySqlConnection objConn = new MySqlConnection(ConfigurationSettings.AppSettings["connString"]);
            string sqlQuery = "SELECT * FROM yourtable ";
            MySqlCommand objCmd = new MySqlCommand(sqlQuery, objConn);
            MySqlDataAdapter da = new MySqlDataAdapter();
            DataSet ds = new DataSet();
          da.SelectCommand = objCmd;
          da.Fill(ds, "MyTable");
            Myrepeater.DataSource = ds.Tables["MyTable"];
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "">

    <asp:Repeater ID="Myrepeater" runat="server">
    <ItemTemplate><%# DataBinder.Eval(Container.DataItem, "FeildName")%></ItemTemplate>

    LVL 35

    Expert Comment

    Of course there are other ways to do it, including binding the dataset directly, geting a dataReader, etc depending on what you want to be doing....

    Author Comment

    I presume this is for an  page? What I am trying to do is make a stand alone program on the computer using c# that when it requires information it just goes to my websites database to get it. also new information is then sent to and updates the site. I wrote my website in PHP. So is it possible to just connect to the DB, someone said with an OBDC I think. As a later project I am going to have a temperature sensor which is connected via serial port that sends current temperature to my websites database. Then the website displays this. Thats as a later project though.
    LVL 4

    Expert Comment

    try to think about using myODBC rathen connect mySQL directly! you can download myODBC from or
    LVL 13

    Accepted Solution

    Solution 1:

    You can go to this website to download MySQLDirect .NET Data Provider

    then you can use following code
    // using name space
    using eInfoDesigns.dbProvider.MySqlClient;
    // create connection
    MySqlConnection oMySqlConn = new MySqlConnection();
    oMySqlConn.ConnectionString = "my connectionstring";

    Solution 2: using OleDB

    OleDbConnection cn = new OleDbConnection( "Provider=MySQLProv;Data Source=mydb;User Id=UserName;Password=asdasd;");

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now