简体   繁体   English

C# 连接数据库

[英]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.我正在尝试使用 c# 创建一个程序,该程序需要连接到运行在 Solaris 服务器上的数据库,我对服务器不太熟悉,我们通常使用 dbVisualizer 来连接它。 the driver it uses to connect is mysql-connector-java-5.1.10, which is a jdbc driver.它用来连接的驱动程序是mysql-connector-java-5.1.10,它是一个jdbc驱动程序。 was wondering what drivers to use to connect to the database using C# and what is the syntax used to establish the connection.想知道使用什么驱动程序来使用 C# 连接到数据库,以及用于建立连接的语法是什么。 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#.如果我正确阅读了您的问题,您正在尝试从 c# 连接到 MySql 数据库。 This can be achieved by downloading the .net connector for MySql - Connector/Net .这可以通过下载 MySql - Connector/Net的 .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.当您安装此驱动程序时,它将与 Visual Studio “集成”,您将能够直接从 Visual Studio 和将使用该驱动程序的程序连接到服务器。

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 .关于连接语法的问题,您将需要使用 MySqlConnection,这里有一个教程 - http://bitdaddys.com/MySQL-ConnectorNet.html ,或者使用类似ZEB0DE80DE53AC5DCBC212BBAA208之类的东西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.您将该字符串传递给没有任何 JDBC:// 前缀的连接。

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.请注意,有一段时间没有这样做了,所以连接字符串可能是错误的(如果我错了,请纠正我),如果您以后忘记任何连接字符串,您可以随时使用http://www 之类的网站。 connectionstrings.com/为您显示所有内容。 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我相信这是您想要连接的(在服务器上): 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.).您可能希望遵循 IDisposable 类的常规准则(使用 using 等)。

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(); { 尝试 { connexion.Open(); return true;返回真; } catch (MySqlException ex) { switch (ex.Number) { case 0: MessageBox.Show("Cannot connect to server. Contact administrator"); } catch (MySqlException ex) { switch (ex.Number) { case 0: MessageBox.Show("无法连接到服务器。请联系管理员"); break;休息; case 1045: MessageBox.Show("Invalid username/password, please try again"); case 1045: MessageBox.Show("用户名/密码无效,请重试"); break;休息; } return false; } 返回假; } } } }

    public bool ColseConnexion()

{ try { connexion.Close(); { 尝试 { connexion.Close(); return true;返回真; } catch (MySqlException ex) { MessageBox.Show(ex.Message); } catch (MySqlException ex) { MessageBox.Show(ex.Message); return false;返回假; } } } }

}   

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM