|
-
July 2nd, 2009, 07:20 AM
#1
Problems connection to MySql database (remote)
I am currently writing some software and I need to be able to connect to a MySql database on a remote server, i've spent hours trying different thing but I just can't seem to crack it.
Here is the code that i'm using to -
#region Building the connection string
string Server = "http://student.dc.lincoln.ac.uk";
string Username = "edited";
string Password = "edited";
string Database = "u08105199";
string ConnectionString = "Data Source=" + Server + ";";
ConnectionString += "User ID=" + Username + ";";
ConnectionString += "Password=" + Password + ";";
ConnectionString += "Initial Catalog=" + Database;
#endregion
#region Try to establish a connection to the database
SqlConnection SQLConnection = new SqlConnection();
try
{
SQLConnection.ConnectionString = ConnectionString;
SQLConnection.Open();
MessageBox.Show("Great Success!");
// You can get the server version
// SQLConnection.ServerVersion
}
catch (Exception Ex)
{
// Try to close the connection
if (SQLConnection != null)
SQLConnection.Dispose();
// Create a (useful) error message
string ErrorMessage = "A error occurred while trying to connect to the server.";
ErrorMessage += Environment.NewLine;
ErrorMessage += Environment.NewLine;
ErrorMessage += Ex.Message;
// Show error message (this = the parent Form object)
MessageBox.Show(this, ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
// Stop here
return;
}
#endregion
Any help with this would be greatly appreciated.
- James
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|