C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,406 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,593 people online right now. Registration is fast and FREE... Join Now!




MySQL Connection with C#

 

MySQL Connection with C#

golgor

2 Jul, 2009 - 06:26 AM
Post #1

New D.I.C Head
*

Joined: 2 Jul, 2009
Posts: 3

Hey, I got recently started with learning some C# and ran into some trouble getting a database connection to work. I read guides etc and made my own class but always got this wierd error. Then i figured if i copied a class from the net, which other people got to work. I hoped it would work for me too and then i could find the difference in my code. This did not work either...

1: I get one error that the class have made an connection but cant login. I checked the password and user very carefully of course. Used another application "Navicat" to connect and add databases etc. Copied both username and the password straight from that application ie. i know the password and user are correct!

2: Sometimes, without ANY changes in the code i get the second error. Non-negative number which i have no idea what it means or why.

I have searched alot of forums and used google but i cant find any answers about these problems that have with sql connection to do.
If you have any suspicion or you know what these are. Please show me in the right direction.

Thanks
Golgor

Code ripped of the net (Only for the purpose of learning)

public void makeDBconn()
{
// This example needs the
// System.Data.SqlClient library

string Server = "localhost,3306";
string Username = "root";
string Password = "";
string Database = "sunklan";

// Put together the connectionstring
string ConnectionString = "Data Source=" + Server + ";";
ConnectionString += "User ID=" + Username + ";";
ConnectionString += "Password=" + Password + ";";
ConnectionString += "Initial Catalog=" + Database;

SqlConnection SQLConnection = new SqlConnection();

try
{
SQLConnection.ConnectionString = ConnectionString;
SQLConnection.Open();

// You can get the server version
// SQLConnection.ServerVersion
}
catch (Exception Ex)
{
// Try to close the connection
if (SQLConnection != null)
SQLConnection.Dispose();

// Show error message (this = the parent Form object)
MessageBox.Show(Ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

// Stop here
return;
}
SQLConnection.Close();
}



Attached thumbnail(s)
Attached Image Attached Image
User is offlineProfile CardPM
+Quote Post


RudiVisser

RE: MySQL Connection With C#

2 Jul, 2009 - 08:01 AM
Post #2

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
Quick question slightly-off topic, have you considered using the MySQL Connector .NET?
User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: MySQL Connection With C#

2 Jul, 2009 - 08:19 AM
Post #3

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,714



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Take a look at this snippet. It uses the MySQl Connector which can be found here
User is offlineProfile CardPM
+Quote Post

golgor

RE: MySQL Connection With C#

2 Jul, 2009 - 09:09 AM
Post #4

New D.I.C Head
*

Joined: 2 Jul, 2009
Posts: 3

QUOTE(PsychoCoder @ 2 Jul, 2009 - 08:19 AM) *

Take a look at this snippet. It uses the MySQl Connector which can be found here


Big thanks PsychoCoder! Seems I mixed up SQL and MySQL. Everything seems to work so far, just tried open and close connection and i didnt get a big errorbox in my face. smile.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: MySQL Connection With C#

2 Jul, 2009 - 09:11 AM
Post #5

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,714



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Glad I could help smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:43PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month