我在一个类中涉及使用MySQL ODBC 5.3 ANSI驱动程序连接到数据库。通过Web界面连接到数据库时,我的凭据没有问题。但是,尝试通过Visual Studio 2013访问数据库时收到错误。
首先,我将链接我们用于连接数据库的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//Add namespaces needed for database connection
using System.Data;
using System.Data.Odbc;
//Project Lab #1
//By Jane Doe
//SAI 430 - Prof. Smith
//November 11, 2013
namespace ProjectLab1
{
class Program
{
static void Main(string[] args)
{
//Connection string needed to talk to Omnymbus
string conString = "Driver={MySQL ODBC 5.3 ANSI Driver};"
+ "Server=devry.edupe.net;Port=4300;"
+ "Database=Inventory_4264;"
+ "uid=4264;pwd=****";
OdbcConnection connection = new OdbcConnection(conString);
connection.Open();
//Get all data
string theQuery = "SELECT * FROM item";
OdbcDataAdapter DataAdapter = new OdbcDataAdapter(theQuery, connection);
DataSet theData = new DataSet();
DataAdapter.Fill(theData, "item");
DataTable theTable = theData.Tables[0];
//Loop through all data results
foreach (DataRow dataRow in theTable.Rows)
{
Console.WriteLine(dataRow["item_id"].ToString());
}
connection.Close(); //Close connection
//Have program pause to keep from closing console window
Console.WriteLine("All Done - Press ENTER");
Console.ReadLine();
}
}
}
执行此代码时,出现以下错误:
“System.Data.Odbc.OdbcException”类型的未处理异常 发生在System.Data.dll
中其他信息:错误[HY000] [MySQL] [ODBC 5.3(a) 驱动程序]访问被拒绝用户'4264'@'71.57.101.56'(使用密码: YES)
我已尝试通过管理工具直接连接到数据库>数据源,但没有运气,有相同的错误。我尝试过同时使用32位和64位版本的连接器,结果没有区别。这真的给了我一个困难的时间,班上的讲师很难提供任何有效的解决方案。
我感谢大家提供的任何见解
答案 0 :(得分:0)
当MySQL不允许外部连接时,可能会发生此错误。
默认只接受来自localhost的连接。
执行此操作的安全方法是通过ssh隧道(在Linux上)进行连接并执行端口转发。否则,您必须允许用户从其他设备连接。
见这里:http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
出于安全原因,不建议使用远程连接!