C# connecting to a database
I Am Trying to create a program using c# that needs to connect to a database running on a Solaris server, I am not too familiar with the server, we normally use dbVisualizer to connect to it. the driver it uses to connect is mysql-connector-java-5.1.10, which is a jdbc driver. was wondering what drivers to use to connect to the database using C# and what is the syntax used to establish t开发者_JS百科he connection. as far as I know I will be unable to install any drivers on the server side, and i will only be able to make changes/Install what is required on the client.
If I read your question correctly you are trying to connect to a MySql database from c#. This can be achieved by downloading the .net connector for MySql - Connector/Net. When you install this driver it will "integrate" with Visual Studio and you will be able to connect to the server directly from Visual Studio and your Program that will use the driver.
On the question on the syntax to connect you will either need to use MySqlConnection, with a tutorial here - http://bitdaddys.com/MySQL-ConnectorNet.html, or use something like the ADO.NET Entity Framework. But that depends on your Tastes.
I am assuming this Server can be access over the network.
Update User Confused about Connection String
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
You pass that string to the connection without any JDBC:// prefixes.
Please Note haven't done this in a while so the connection string could be wrong (So correct me If I'm wrong) and if you forget any connection string in the future you can always use a website like http://www.connectionstrings.com/ which shows them all for you. That is where I got the string above.
Hope that helps.
I believe this is what you want to connect (on the server): http://dev.mysql.com/downloads/connector/net/1.0.html
You can try your connection like this:
string MyConString = "SERVER=yourserver;" +
"DATABASE=mydatabase;" +
"UID=testuser;" +
"PASSWORD=testpassword;";
MySqlConnection connection = new MySqlConnection(MyConString);
You would probably want to follow the normal guidelines for IDisposable classes (use using etc.).
using MySql.Data.MySqlClient;
using System.Windows;
class Connexion
{
public MySql.Data.MySqlClient.MySqlConnection connexion;
private string server;
private string database;
private string uid;
private string password;
public Connexion()
{
server = "localhost";
database = "GestionCommeriale";
uid = "root";
password = "";
String connexionString;
connexionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" +
"UID" + uid + ";" + "PASSSWORD =" + password + ";";
connexion = new MySqlConnection(connexionString);
}
public bool OpenConnexion()
{ try { connexion.Open(); return true; } catch (MySqlException ex) { switch (ex.Number) { case 0: MessageBox.Show("Cannot connect to server. Contact administrator"); break; case 1045: MessageBox.Show("Invalid username/password, please try again"); break; } return false; } }
public bool ColseConnexion()
{ try { connexion.Close(); return true; } catch (MySqlException ex) { MessageBox.Show(ex.Message); return false; } }
}
}
精彩评论