• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

connecting to mysql DB

I am very new to C# and programming. I am using Visual studio.net 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.
0
jdav3579
Asked:
jdav3579
1 Solution
 
mrichmonCommented:
In the web.config file for your website

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


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"];
      Myrepeater.DataBind();
        objConn.Close();
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD><title>Test</title></HEAD>
<BODY>

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

</BODY>
</HTML>
0
 
mrichmonCommented:
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....
0
 
jdav3579Author Commented:
I presume this is for an asp.net  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.
0
 
boy8964Commented:
try to think about using myODBC rathen connect mySQL directly! you can download myODBC from mysql.org or mysql.com
0
 
dunglaCommented:
Solution 1:

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

http://crlab.com/mysqlnet/

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

==========
Solution 2: using OleDB

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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now