【发布时间】:2012-03-07 03:47:20
【问题描述】:
我使用 WebMatrix 编写我的站点 (C#/MVC3),并且我使用本地 mysql 服务器(不是通过 webmatrix 安装)来存储我的所有数据,所以我尝试转到 Connections 选项卡并在其中添加我的服务器作为New Connection。但无论我为Host 输入什么,它都会给我一个错误。我知道数据库名称、用户名和密码是正确的。
web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<connectionStrings>
<add connectionString="Uid=username;Server=localhost;Database=mydb;Pwd=pass" name="localhost" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
代码:
@{
Page.Title = "Comics";
var db = Database.Open("localhost");
var comics = db.Query(@"SELECT * FROM comics ORDER BY arc ASC").ToList();
}
<h1>@Page.Title</h1>
<ul class="thumbnails gallery">
@foreach (var comic in comics) {
<li>@comic.Arc</li>
}
</ul>
错误
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
非常感谢任何帮助!
编辑:
我安装了 MySQL 连接器,但仍然出现错误:
Unable to find the requested .Net Framework Data Provider. It may not be installed.
我从http://dev.mysql.com/downloads/mirror.php?id=403991 下载了它,并使用默认设置安装了它。我应该将它安装到 webmatrix 目录中吗?!?
新的 web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<connectionStrings>
<add connectionString="Server=localhost;Database=mydb;Uid=user;Pwd=pass;" name="localhost" providerName="MySql.Data.MySqlClient.MySqlConnection" />
</connectionStrings>
</configuration>
【问题讨论】: