我最近从经典的ASP跳到了ASP.Net,连接到测试数据库时遇到了问题。
我使用的是Visual Studio 2012和MySQL 5.6,并遵循本教程。
我创建了一个测试连接成功的UDL文件,在我的应用程序中,我在连接字符串中添加了以下内容(这与我在UDL中使用的连接字符串相同):
<connectionStrings>
<add name="MyConn" connectionString="DRIVER={MySQL ODBC 5.2 Unicode Driver};Database=Zen;Server=localhost;UID=root;pwd=*****;" />
</connectionStrings>
这与我在创建UDL文件时使用的连接字符串相同。
我的应用程序在运行时抛出以下异常:
出现错误:错误[IM002][Microsoft][ODBC驱动程序管理器]找不到数据源名称,并且未指定默认驱动程序。
我不知道这是否会有所帮助,但我的cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;
using System.Configuration;
using MySql.Data;
namespace Zen
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
using(OdbcConnection connection = new OdbcConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString))
{
connection.Open();
using(OdbcCommand command = new OdbcCommand("SELECT name FROM test_users", connection))
using(OdbcDataReader dr = command.ExecuteReader())
{
while(dr.Read())
Response.Write(dr["name"].ToString() + "<br />");
dr.Close();
}
connection.Close();
}
}
catch(Exception ex)
{
Response.Write("An error occured: " + ex.Message);
}
}
}
}
好的。我通过安装32位MySQL ODBC驱动程序解决了这个问题。