无法使用connectionString

时间:2019-11-13 14:11:38

标签: c# mysql .net mysql-workbench

我可能想念一些愚蠢的东西...但是我一直试图整天弄清楚它,所以我放弃了。 谁能发现错误?

App.config

  <connectionStrings>
<add name="linkednDb" connectionString="Server=.;Database=linkedin.usernames;Uid=root;Pwd=1234;" providerName="System.Data.SqlClient"/>

查询方法:

public void AddUser(string username)
    {
        using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.Connection("linkednDb")))
        {
            connection.Query<User>($"INSERT INTO usernames (UserName) VALUES ('{username}')");
            Console.WriteLine("adding" + username);
        }
    }

Helper类:

 public static class Helper
{
    public static string Connection(string name)
    {
     return ConfigurationManager.ConnectionStrings[name].ConnectionString;  
    }
}

the table

忘记添加错误!

System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)'

2 个答案:

答案 0 :(得分:2)

System.Data.SqlClient Sql Server 的ADO.Net提供程序,它是与MySql完全不同的数据库平台。

您需要一个MySql提供程序。该提供程序未随.Net一起提供,但是您可以在Visual Studio中安装和引用a few options out there

虽然我在这里,但SQL代码中的字符串替换为不好。 SQL查询中的字符串替换非常容易受到SQL注入问题的攻击。这是应用程序最终被黑客入侵的主要方式之一。不太恶意的问题也可能发生。这不会引起33t黑客的攻击。普通的老Joe用户只需输入真实数据即可破坏应用程序。您需要学习如何使用参数化查询。

答案 1 :(得分:-1)

在您配置中的connectionString值中,我认为它应该是“ Database = linkedin”而不是“ Database = linkedin.usernames”。表名称对于数据库值不是必需的。

相关问题