如何在C#中使用mysql建立连接字符串 [英] how to make connection string with mysql in C#

查看:958
本文介绍了如何在C#中使用mysql建立连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我必须在C#winform配置文件中建立连接字符串。

因为我有服务器名称=abc

userid = root

且没有密码。



请建议。

Hi All,

I have to make connectionstring in C# winform config file.
in that I have server name="abc"
userid=root
and there is no password .

Please suggest.

推荐答案

我已经粘贴了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


这篇关于如何在C#中使用mysql建立连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆