2015-10-17 43 views
0

我在asp.net中使用了我的web應用程序的mysql。然而,當它在瀏覽器中運行時,它會向我顯示此錯誤。無法將ASP.NET網頁與mysql連接

System.TimeoutException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ---> 
System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ---> 
System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at 
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at 
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at MySql.Data.Common.MyNetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count) --- End of inner exception stack trace --- at 
MySql.Data.Common.MyNetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count) at 
MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count) at 
System.IO.BufferedStream.Read(Byte[] array, Int32 offset, Int32 count) at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count) at 
MySql.Data.MySqlClient.MySqlStream.LoadPacket() at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.Open() at MySql.Data.MySqlClient.Driver.Open() at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings) at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() at MySql.Data.MySqlClient.MySqlPool.GetConnection() at MySql.Data.MySqlClient.MySqlConnection.Open() at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at 
System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) at 
school.Page_Load(Object sender, EventArgs e) in 
E:\VIDYAN\school.aspx.vb:line 20 

連接字符串

<add name="constr" connectionString="Data Source=localhost;port=81;Initial Catalog=Vidyan;User Id=root;password="/> 

VB代碼

Imports System.Data 
Imports System.Configuration 
Imports MySql.Data.MySqlClient 
Partial Class school 
    Inherits System.Web.UI.Page 

    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString 
    Dim query, str As String 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     Try 
      ViewState("Data") = "" 
      Using con As New MySqlConnection(constr) 
       Using cmd As New MySqlCommand("SELECT * FROM school") 
        Using sda As New MySqlDataAdapter() 
         cmd.Connection = con 
         sda.SelectCommand = cmd 
         Using dt As New DataTable() 
          sda.Fill(dt) 
          ViewState("Data") = dt 
          schoollists.DataSource = dt 
          schoollists.DataBind() 

         End Using 
        End Using 
       End Using 
      End Using 

     Catch ex As Exception 
      Response.Write(ex) 
     End Try 
    End Sub 

    Protected Sub DataPager1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender 
     Try 
      Dim table As DataTable = ViewState("Data") 
      schoollists.DataSource = table 
      schoollists.DataBind() 

     Catch ex As Exception 
      'Response.Write(ex) 
     End Try 

    End Sub 
End Class 
+0

@wingᴇdpᴀnᴛʜᴇʀ嘗試了這一點,並再次出現錯誤。在我的問題更新.. –

+1

第一個問題,這是本地主機還是遠程?第二個問題,你檢查過防火牆嗎?連接超時是防火牆的典型行爲 – Neo

+0

@Neo它是本地主機連接。 mysql在wamp服務器下運行。而fiewall是關閉的。 –

回答

1

MySQL的默認端口是3306,在連接字符串中它是81,我會檢查端口和WAMP嘗試3306 :)