derekthornton
asked on
C# and MySQL
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.
see http://www.able-consulting.com/dotnet/adonet/Data_Providers.htm#MySQLDirectNETDataProvider
ASKER
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?
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.My SqlConnect ion con = new MySqlConnection("Datasourc e=YOURSERV ER;Databas e=YOURDATA BASE;User ID=YOURUSER;Password=PASSW ORD;");
con.Open();
ByteFX.Data.MySqlClient.My SqlCommand myCommand = con.CreateCommand();
myCommand.CommandText = "YOUR QUERY";
ByteFX.Data.MySqlClient.My SqlDataRea der Reader = myCommand.ExecuteReader();
while(Reader.Read())
{
MessageBox.Show(Reader.Get String(0)) ;
}
con.Close();
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.My
con.Open();
ByteFX.Data.MySqlClient.My
myCommand.CommandText = "YOUR QUERY";
ByteFX.Data.MySqlClient.My
while(Reader.Read())
{
MessageBox.Show(Reader.Get
}
con.Close();
ASKER
Hrnm. Doesn't seem to work.
can you not use a remote server running MySQL?
can you not use a remote server running MySQL?
Post the code you are using... I have used this before for a remote server and it worked fine.
ASKER
ByteFX.Data.MySqlClient.My SqlConnect ion con = new ByteFX.Data.MySqlClient.My SqlConnect ion("Datas ource=www.mydomainname.com;Database=beta_public_tracker;User ID=php_admin;Password=pass word;");
con.Open();
ByteFX.Data.MySqlClient.My SqlCommand myCommand = con.CreateCommand();
myCommand.CommandText = "YOUR QUERY";
ByteFX.Data.MySqlClient.My SqlDataRea der Reader = myCommand.ExecuteReader();
while(Reader.Read())
{
MessageBox.Show(Reader.Get String(0)) ;
}
con.Close();
con.Open();
ByteFX.Data.MySqlClient.My
myCommand.CommandText = "YOUR QUERY";
ByteFX.Data.MySqlClient.My
while(Reader.Read())
{
MessageBox.Show(Reader.Get
}
con.Close();
ASKER
the datasource is just the path to my domain name, right?
Data Source is your server name or ip address.
ASKER
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.
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.
Post the code, let me look at it.
ASKER
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?
The server runs on Linux, is that going to stop it?
Sorry, didn't even notice it was there.
Do you know what port your database is using?
Do you know what port your database is using?
ASKER
What port does MySQL usually run on?
3306 i believe
Have you ever connected to the database from your computer before using the address you specified?
ASKER
Well, I haven't changed anything, so it must be running on that port. What does that do for me?
ASKER
Not through code, no. But through other applications I have.
Try putting the ip address in the connection string instead.
Then you can try the ip and port...
192.168.1.1:3306
Then you can try the ip and port...
192.168.1.1:3306
ASKER
I don't understand what you mean.
ByteFX.Data.MySqlClient.My SqlConnect ion con = new ByteFX.Data.MySqlClient.My SqlConnect ion("Datas ource=192. 168.1.1:33 06;Databas e=beta_pub lic_tracke r;User ID=php_admin;Password=pass word;");
con.Open();
ByteFX.Data.MySqlClient.My SqlCommand myCommand = con.CreateCommand();
myCommand.CommandText = "YOUR QUERY";
ByteFX.Data.MySqlClient.My SqlDataRea der Reader = myCommand.ExecuteReader();
while(Reader.Read())
{
MessageBox.Show(Reader.Get String(0)) ;
}
con.Close();
con.Open();
ByteFX.Data.MySqlClient.My
myCommand.CommandText = "YOUR QUERY";
ByteFX.Data.MySqlClient.My
while(Reader.Read())
{
MessageBox.Show(Reader.Get
}
con.Close();
ASKER
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?
your server ip address.
ASKER
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 ....
I'm using WebSiteSource. It's been awful since the day I signed up ....
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.
ASKER
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.
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.
ASKER
Okay. Server is back up. The code you gave me doesn't fix it.
Specifically what error are you getting? Try and include the entire error.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It just says "Unable to retrieve any of the MySQL Data Sources"
http://sourceforge.net/projects/mysqldrivercs/
I used this at work last year. It works, but I can't say I love its interface.
I used this at work last year. It works, but I can't say I love its interface.
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 ","").AsSt ring );
MessageBox.Show("Connectin g to database");
try
{
conn.Open();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message );
return;
}
If you need more samples, install the MySQLDriverCS, and look in the source -> samples directory.
MySQLConnection conn = new MySQLConnection( new MySQLConnectionString ("localhost","mysql","root
MessageBox.Show("Connectin
try
{
conn.Open();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message
return;
}
ASKER
An unhandled exception of type 'ByteFX.Data.MySqlClient.M ySqlExcept ion' occurred in bytefx.mysqlclient.dll
Additional information: Unable to connect to any of the specified MySQL hosts
That's the error I get, gillgates
Additional information: Unable to connect to any of the specified MySQL hosts
That's the error I get, gillgates
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>
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>
ASKER
That's generated automatically by the compiler.
ASKER
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.
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.
ASKER
Does ByteFX only work for MySQL 4? I'm on 3.2
No it should work for 3.2, I do know that it doesn't work on the .Net Framework 1.0 only 1.1
ASKER
What about user permissions? What should those be in my SQL user permissions?
ASKER
Woot!I got it!
The problem was that my server hadn't opened the port for me on the firewall.
The problem was that my server hadn't opened the port for me on the firewall.
Thats great... I knew it had to be something like that...
Have fun.
Have fun.