【发布时间】:2020-03-09 08:45:07
【问题描述】:
我可能错过了一些愚蠢的东西......但我整天都在试图弄清楚,所以我放弃了。 谁能发现错误?
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);
}
}
助手类:
public static class Helper
{
public static string Connection(string name)
{
return ConfigurationManager.ConnectionStrings[name].ConnectionString;
}
}
忘记添加错误!
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)'
【问题讨论】:
-
错误信息是什么
-
您是否收到错误消息、意外结果或其他问题?你在使用 Dapper 吗? Dapper doesn't use string interpolation 因为这是个坏主意。
For Dapper, this remains something we wouldn't add - it's not worth the safety trade-offs :)您正在执行的不是参数化查询,而是动态生成的字符串,该字符串很容易无效 -
比如用户名是
O'Reilly怎么办?你会得到INSERT INTO usernames (UserName) VALUES ('O'Reilly')和一个无效的查询错误 -
System.Data.SqlClient是 SQL Server 客户端。你不能用它来连接MySQL -
@fubo 是的,问题标记为
mysql,而代码使用的是MySQL连接字符串。
标签: c# mysql .net mysql-workbench