我试图通过网页创建一个简单的数据库加载连接,但它总是给我一个错误:"建立与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
答案 0 :(得分:0)
如果要连接到MySql,则需要使用相应的类
SqlConnection和SqlDataAdapter是连接到Microsoft Sql Server所需的类,对于MySql,您需要MySqlConnection
和MySqlDataAdapter
。
如果您下载并安装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