且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中使用mysql建立连接字符串

更新时间:2023-02-17 08:33:55

我已经粘贴了msdn页面提供的如何执行此操作的代码。有关进一步参考,请查看底部的链接。在GetConnectionString()处更改返回的字符串,或者将其全部替换为一个简单的字符串。这取决于你和你所面临的情况。



I've pasted the code from how to do this that is available at the msdn page. For further reference have a look at the link at the bottom. Change the returning string at GetConnectionString() or replace it all together with a simple string. It's up to you and the situation you are facing.

private static void OpenSqlConnection()
{
    //sets the connection string
    string connectionString = GetConnectionString();

    //makes sure the connetion will be disposed
    using (SqlConnection connection = new SqlConnection())
    {
        connection.ConnectionString = connectionString;

        connection.Open();

        Console.WriteLine("State: {0}\nConnectionString: {1};", connection.State, connection.ConnectionString);
    }
}

static private string GetConnectionString()
{
    // To avoid storing the connection string in your code,
    // you can retrieve it from a configuration file.
    return "Data Source=YourDataSource;Initial Catalog=yourCatalog;Integrated Security=true;";
}





来源:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs .110).aspx [ ^ ]



问候



Source: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx[^]

Regards