如何修复Web连接无法从我的数据库加载数据

时间:2014-12-16 23:56:38

标签: mysql asp.net vb.net

我试图通过网页创建一个简单的数据库加载连接,但它总是给我一个错误:"建立与SQL Server的连接时发生网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供者:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)"

这是我的web.config,使用MySQL,MySQL服务器2005

<connectionStrings>
<add name="MySQLConnection" connectionString="database=peronrecords;server=localhost;uid=root;pwd=root"/>
</connectionStrings>

这是我使用gridview的vb代码:

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("MySQLConnection").ToString())
        ' Create a DataSet object. 
        Dim da As SqlDataAdapter
        Dim dt As New DataTable

        da = New SqlDataAdapter("SELECT id, name FROM student", conn)
        da.Fill(dt)

        ' Open the connection 
        conn.Open()

        GridView1.DataSource = dt
        GridView1.DataBind()
    End Using
End Sub

1 个答案:

答案 0 :(得分:0)

如果要连接到MySql,则需要使用相应的类 SqlConnection和SqlDataAdapter是连接到Microsoft Sql Server所需的类,对于MySql,您需要MySqlConnectionMySqlDataAdapter

如果您下载并安装MySql NET Connector,然后将add a reference下载并安装到MySql.Data.dll,并为MySql.Data.MySqlClient添加Imports statement,则可以找到这些类。你的代码文件。

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Using conn As New MySqlConnection(ConfigurationManager.ConnectionStrings("MySQLConnection").ToString())
        ' Create a DataSet object'
        Dim da As MySqlDataAdapter
        Dim dt As New DataTable

        da = New MySqlDataAdapter("SELECT id, name FROM student", conn)
        da.Fill(dt)

        ' Not needed. The DataAdapter opens the connection by itself if it is not opened
        ' conn.Open()

        GridView1.DataSource = dt
        GridView1.DataBind()
    End Using
End Sub