WebMatrix + MySQL链接

时间:2012-03-07 03:47:20

标签: mysql webmatrix

我使用WebMatrix编写我的站点(C#/ MVC3),我使用的是本地mysql服务器(未通过webmatrix安装)来存储我的所有数据,所以我尝试转到Connections选项卡并在那里添加我的服务器作为New Connection。但无论我为Host投入什么,它只会给我一个错误。我知道db名称,用户名和&密码是正确的。

的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>

1 个答案:

答案 0 :(得分:2)

无论您使用的是MySQL(如帖子的标题和您选择的标签)还是Sql Server(如连接字符串中),您都需要下定决心。如果您使用的是MySQL,则需要使用不同的连接字符串。

http://www.connectionstrings.com/mysql