Solved

C# and MySQL

Posted on 2004-08-17
42
409 Views
Last Modified: 2008-01-09
Simple question. Can C# even use MySQL? If So, how? I've not been able to find many tutorials covering this, or even specifying if it can really be done.
0
Comment
Question by:derekthornton
  • 21
  • 17
  • 2
  • +2
42 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 11820343
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11821345
yes you can

http://www.bytefx.com

This is what mysql suggests I believe.
0
 
LVL 19

Expert Comment

by:Desp
ID: 11822052
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11822266
Yes. The ByteFX seems to be the best solution ...but I'm not entirely sure how it works. the Documentation is REALLY poor IMO as it gives absolutely no examples .... how does it just ..plain connect to a DB on a Remote Server?
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11822928
Download an install
Add a refernces to bytefx.mysqlclient.dll.  In solution explorer right click on "References" and select "add"  From the list select "ByteFX.MySqlClient.dll"  Press Select and Ok.


using ByteFX.Data.MySqlClient;

-----------------

ByteFX.Data.MySqlClient.MySqlConnection con = new MySqlConnection("Datasource=YOURSERVER;Database=YOURDATABASE;User ID=YOURUSER;Password=PASSWORD;");

con.Open();
ByteFX.Data.MySqlClient.MySqlCommand myCommand = con.CreateCommand();
myCommand.CommandText = "YOUR QUERY";
ByteFX.Data.MySqlClient.MySqlDataReader Reader = myCommand.ExecuteReader();

while(Reader.Read())
{
   MessageBox.Show(Reader.GetString(0));
}

con.Close();
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11823197
Hrnm. Doesn't seem to work.

can you not use a remote server running MySQL?
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11823240
Post the code you are using... I have used this before for a remote server and it worked fine.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11823270
                 ByteFX.Data.MySqlClient.MySqlConnection con = new ByteFX.Data.MySqlClient.MySqlConnection("Datasource=www.mydomainname.com;Database=beta_public_tracker;User ID=php_admin;Password=password;");

                  con.Open();
                  ByteFX.Data.MySqlClient.MySqlCommand myCommand = con.CreateCommand();
                  myCommand.CommandText = "YOUR QUERY";
                  ByteFX.Data.MySqlClient.MySqlDataReader Reader = myCommand.ExecuteReader();

                  while(Reader.Read())
                  {
                        MessageBox.Show(Reader.GetString(0));
                  }

                  con.Close();
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11823274
the datasource is just the path to my domain name, right?
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11823283
Data Source is your server name or ip address.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11823302
Yeah. That doesn't work. Am I putting in the strings wrong or something? do they need quotes or something?
Or the database name maybe?

I contacted my Hosting Provider and they said my domain name would be what I use, but this code just doesn't connect.
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11823312
Post the code, let me look at it.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11823322
I did. I can't post my password and stuff on a public board.
The server runs on Linux, is that going to stop it?
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11823349
Sorry, didn't even notice it was there.

Do you know what port your database is using?
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11823360
What port does MySQL usually run on?
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11823415
3306 i believe
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11823423
Have you ever connected to the database from your computer before using the address you specified?
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11823425
Well, I haven't changed anything, so it must be running on that port. What does that do for me?
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11823429
Not through code, no. But through other applications I have.
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11823445
Try putting the ip address in the connection string instead.

Then you can try the ip and port...

192.168.1.1:3306
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11823456
I don't understand what you mean.
0
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.

 
LVL 3

Expert Comment

by:gillgates
ID: 11823460
            ByteFX.Data.MySqlClient.MySqlConnection con = new ByteFX.Data.MySqlClient.MySqlConnection("Datasource=192.168.1.1:3306;Database=beta_public_tracker;User ID=php_admin;Password=password;");

               con.Open();
               ByteFX.Data.MySqlClient.MySqlCommand myCommand = con.CreateCommand();
               myCommand.CommandText = "YOUR QUERY";
               ByteFX.Data.MySqlClient.MySqlDataReader Reader = myCommand.ExecuteReader();

               while(Reader.Read())
               {
                    MessageBox.Show(Reader.GetString(0));
               }

               con.Close();
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11823472
But 192.168.1.1 isn't the right address ..should I just substitute with my server IP? or is there a reason why you chose that IP?
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11823491
your server ip address.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11823511
Nope. That didn't work, but I think my hosting just went down. They've been horrible.
I'm using WebSiteSource. It's been awful since the day I signed up ....
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11823610
Well, when it comes back up let me know... Once I get home I will be able to possibly help more because that is where all my mysql code is.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11823623
Thanks.

I'm wanting to write a Chat Program that connects to a Jabber Server (which I have running on the server already) and stores some information (it has nothing to do with the jabber server, the jabber server is just the chat portion) in MySQL o nthe server and can use that info for some various stuff. So yeah ..hopefully I can make MySQL and C# talk.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11824185
Okay. Server is back up. The code you gave me doesn't fix it.
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11824431
Specifically what error are you getting?  Try and include the entire error.
0
 
LVL 3

Accepted Solution

by:
gillgates earned 250 total points
ID: 11825985
Try this
__________________________

using ByteFX.Data.MySqlClient;

--------------

string DataSource = "yourserver";
string Database ="public_beta_tracker"
string UserID = "php_admin";
string Password = "PASSWORD"
string CommandText;
MySqlConnection mysqlCon;
MySqlReader msqlReader;


strConnection  = "Data Source=" + DataSource + ";Database=" + Database + ";User ID=" + UserID + ";Password=" + Password;

msqlCon = new MySqlConnection(strConnection);
            
msqlCon.Open();

strCommandText = @"SELECT * FROM sometable";
                        
msqlCommand = new MySqlCommand(strCommandText, msqlCon);
msqlReader = msqlCommand.ExecuteReader();

while(msqlReader.Read())
{
    MessageBox.Show(msqlReader.GetString(0));
}
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11826120
It just says "Unable to retrieve any of the MySQL Data Sources"
0
 
LVL 2

Expert Comment

by:davidastle
ID: 11826186
http://sourceforge.net/projects/mysqldrivercs/

I used this at work last year.  It works, but I can't say I love its interface.
0
 
LVL 2

Expert Comment

by:davidastle
ID: 11826219
Here is in example of how to connect with this API.
If you need more samples, install the MySQLDriverCS, and look in the source -> samples directory.

MySQLConnection conn =  new MySQLConnection( new MySQLConnectionString  ("localhost","mysql","root","").AsString );
                  MessageBox.Show("Connecting to database");
                  try
                  {
                        conn.Open();
                  }
                  catch(Exception ex)
                  {
                        MessageBox.Show(ex.Message);
                        return;
                  }
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11826643
An unhandled exception of type 'ByteFX.Data.MySqlClient.MySqlException' occurred in bytefx.mysqlclient.dll

Additional information: Unable to connect to any of the specified MySQL hosts

That's the error I get, gillgates
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11826689
I found this at http://bugs.mysql.com/bug.php?id=3866
I don't know if it works but looks easy enough to try.

Try creating a file called appname.exe.config in the same folder as your .exe file....
So it will bin in projectfolder\bin\debug or projectfolder\bin\release

for "appname" put the name of your exe, so if it is called x.exe then the file should be x.exe.config

int the file put...

<?xml version ="1.0"?>
<configuration>
<startup>
   <supportedRuntime version="v1.1.4322" />
</startup>
</configuration>
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11826703
That's generated automatically by the compiler.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11826753
Yeah, thanks for the tip, but it doesn't work.
That tip is only to force it to compile into .NET 1.1 if you have VS2002 installed, which I don't.

The same thing can be accomplished by setting the Runtime version in the properties of any project. It generates that file the same way.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11826856
Does ByteFX only work for MySQL 4? I'm on 3.2
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11829700
No it should work for 3.2, I do know that it doesn't work on the .Net Framework 1.0 only 1.1
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11832364
What about user permissions? What should those be in my SQL user permissions?
0
 
LVL 2

Author Comment

by:derekthornton
ID: 11832536
Woot!I got it!

The problem was that my server hadn't opened the port for me on the firewall.
0
 
LVL 3

Expert Comment

by:gillgates
ID: 11833395
Thats great... I knew it had to be something like that...

Have fun.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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