不与受信任的 SQL Server 连接 (MySQL) 关联
我在连接 MySQL 服务器数据库时遇到问题,并收到以下错误:
错误消息
用户“root”登录失败。原因:未与受信任的 SQL Server 连接关联。
这是发生错误的代码:
public bool IsValid(string username, string password)
{
using (var con = new SqlConnection("Server=localhost;Database = timekeeping; Uid = root; Pwd = admin;"))
using (var cmd = con.CreateCommand())
{
con.Open();
cmd.CommandText = "SELECT count(*) FROM receptionist WHERE username = @username AND password = @password;";
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
var count = (long)cmd.ExecuteScalar();
return count > 0;
}
}
屏幕截图:
这是我的配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ODBCDriver" value="Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=timekeeping;uid=root;pwd=admin;Option=3;"/>
</appSettings>
<connectionStrings>
<add name="connStr" connectionString="Database=timekeeping;uid=root;pwd=admin;Option=3;" />
</connectionStrings>
</configuration>
I'm having trouble connectiong to MySQL Server database and get the following error:
Error Message
Login failed for user 'root'. Reason: Not associated with a trusted SQL Server connection.
This is the code where the error occurred:
public bool IsValid(string username, string password)
{
using (var con = new SqlConnection("Server=localhost;Database = timekeeping; Uid = root; Pwd = admin;"))
using (var cmd = con.CreateCommand())
{
con.Open();
cmd.CommandText = "SELECT count(*) FROM receptionist WHERE username = @username AND password = @password;";
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
var count = (long)cmd.ExecuteScalar();
return count > 0;
}
}
Screenshot:
This is my config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ODBCDriver" value="Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=timekeeping;uid=root;pwd=admin;Option=3;"/>
</appSettings>
<connectionStrings>
<add name="connStr" connectionString="Database=timekeeping;uid=root;pwd=admin;Option=3;" />
</connectionStrings>
</configuration>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用MySQL吗?错误消息明确指出它不与受信任的SQL Server连接关联。我还看到您正在使用SqlConnection(这在SqlCommand、SqlDataAdapter、SqlDataReader等中暗示) ) 来自 System.Data.SqlClient 命名空间 - 特定于 SQL Server:http://msdn.microsoft.com/en-us/library /system.data.sqlclient.sqlconnection.aspx(表示与 SQL Server 数据库的打开连接。)。
要从 .NET 连接到 MySQL 数据库,您可以使用 ODBC(可能还有其他选项),或者如果您想坚持使用 ADO.NET,则可以使用 MySQL 连接器。
我想这应该可以为你解决。如果我没有记错的话,命名空间将是 MySql.Data.MySqlClient,类将被命名为 MySqlConnection 等等。但不确定实际的前缀、命名空间或命名 - 目前在 Mac 上。这里没有 .NET :-D
Are you using MySQL? The error message clearly states it's Not associated with a trusted SQL Server connection. I also see you're using SqlConnection (which then implies in SqlCommand, SqlDataAdapter, SqlDataReader and so on) which comes from the System.Data.SqlClient namespace - which is specific for SQL Server: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx (Represents an open connection to a SQL Server database.).
To connect to your MySQL database from .NET, you can use ODBC (there may be other options) or if you want to stick to ADO.NET, you can use the MySQL Connector.
I guess this should solve it for you. If I'm not mistaken, the Namespace would then be MySql.Data.MySqlClient and the classes would be named MySqlConnection and so on. Not sure about the actual prefixes, namespaces or naming though - on a Mac at the moment. No .NET here :-D
首先,您永远不应该从代码中以“root”身份查询任何数据库(除非其代码执行管理任务;在本例中不是)
您确定“root”密码是“admin”吗? (很难猜到!)
尝试使用已成功连接到数据库的登录名。即您可以使用 MySQL Workbench 连接到数据库的凭据
Firstly, you should never be querying any database from code as 'root' (unless its code that performs admin tasks; which it isn't in this case)
Are you sure the 'root' password is 'admin'? (hard to guess that one!)
Try using a login that you have connected to your DB with successfully. i.e. credentials that you can use MySQL workbench to connect to your DB