【发布时间】:2018-06-07 17:53:24
【问题描述】:
我正在尝试连接到 asp.net 中的 MySQL 数据库(我猜我是菜鸟;p)
我对这件事真的很陌生(意思是 asp)
我正在使用 VS 2013 Pro、MySQL Server 8.0.11,并且我已经为 Visual Studio 1.2.8 安装了 Connector/Net 和 mysql
在服务器资源管理器选项卡中,我与 db 建立了连接,并且我看到了我的表,测试成功(用于 MySQL 的 .NET Framework 数据提供程序和 MySQL 数据库(MySQL 数据提供程序))
但我无法通过代码 (?) 获得连接
当我尝试这个时:
protected void Page_Load(object sender, EventArgs e)
{
try
{
using (OdbcConnection connection = new OdbcConnection(ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString))
{
connection.Open();
using (OdbcCommand command = new OdbcCommand("SELECT imie FROM pracownicy", connection))
using (OdbcDataReader dr = command.ExecuteReader())
{
while (dr.Read())
Response.Write(dr["imie"].ToString() + "<br />");
dr.Close();
}
connection.Close();
}
}
catch (Exception ex)
{
Response.Write("An error occured: " + ex.Message);
}
}
这是 web.config 的一部分
<connectionStrings>
<add name="MySQL" connectionString="Driver={MySQL ODBC 8.0.11 Driver};server=localhost;uid=marcin;pwd=pass;database=test" />
我得到了这样的东西: 出现错误:ERROR [IM002] [Microsoft][ODBC driver manager] 名称数据源无法设置,未提供默认驱动
我也试试这个:
添加了对 MySql.Data.dll 的引用
和代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;
namespace WebApplication1
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection("server=localhost;user id=marcin;persistsecurityinfo=True;database=test;password=pass");
}
}
}
只有我得到了
Warning The primary reference "MySql.Data, Version=8.0.11.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL" could not be resolved because it was built against the ".NETFramework,Version=v4.5.2" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.5".
和
Error The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference?)
我做错了什么?
【问题讨论】: