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();
}