I'm having trouble connectiong to MySQL Server database and get the following error:
我无法连接到MySQL Server数据库并收到以下错误:
Error Message
Login failed for user 'root'. Reason: Not associated with a trusted SQL Server connection.
用户“root”登录失败。原因:与受信任的SQL Server连接无关。
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>
2 个解决方案
#1
2
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.).
你在使用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数据库的打开连接。)。
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.
要从.NET连接到MySQL数据库,可以使用ODBC(可能还有其他选项),或者如果要坚持使用ADO.NET,可以使用MySQL连接器。
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
我想这应该为你解决。如果我没有弄错,那么Namespace将是MySql.Data.MySqlClient,并且这些类将被命名为MySqlConnection,依此类推。目前还不确定实际的前缀,命名空间或命名 - 在Mac上。这里没有.NET :-D
#2
1
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)
首先,你永远不应该从代码中查询任何数据库为“root”(除非它的代码执行管理任务;在这种情况下不是这样)
Are you sure the 'root' password is 'admin'? (hard to guess that one!)
您确定'root'密码是'admin'吗? (很难猜到那个!)
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
尝试使用已成功连接到数据库的登录名。即可以使用MySQL工作台连接到数据库的凭据
#1
2
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.).
你在使用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数据库的打开连接。)。
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.
要从.NET连接到MySQL数据库,可以使用ODBC(可能还有其他选项),或者如果要坚持使用ADO.NET,可以使用MySQL连接器。
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
我想这应该为你解决。如果我没有弄错,那么Namespace将是MySql.Data.MySqlClient,并且这些类将被命名为MySqlConnection,依此类推。目前还不确定实际的前缀,命名空间或命名 - 在Mac上。这里没有.NET :-D
#2
1
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)
首先,你永远不应该从代码中查询任何数据库为“root”(除非它的代码执行管理任务;在这种情况下不是这样)
Are you sure the 'root' password is 'admin'? (hard to guess that one!)
您确定'root'密码是'admin'吗? (很难猜到那个!)
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
尝试使用已成功连接到数据库的登录名。即可以使用MySQL工作台连接到数据库的凭据