2013-10-09 80 views
1

我试图将我的C#应用​​程序连接到Web服务器数据库(mysql)。我正在使用下面的代码。无法从C#应用程序连接远程服务器mysql数据库

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using MySql.Data.MySqlClient; 
namespace mySqlConnectivity 
{ 
    public partial class Form1 : Form 
    { 
     string myConStr; 
     public Form1() 
     { 
      InitializeComponent(); 

      myConStr = "SERVER=xxx.xx.xxx.xx;Port=80;DATABASE=EmSystem;UID=xxx;PASSWORD=xxx;compress=true"; 
     } 


     private void button1_Click(object sender, EventArgs e) 
     { 
      int mobNo; 
      string mobile = textBox3.Text; 
      int.TryParse(mobile, out mobNo); 

      MySqlConnection conn = new MySqlConnection(myConStr); 
      MySqlCommand cmd; 
      conn.Open(); 
      try 
      { 
       cmd = conn.CreateCommand(); 
       cmd.CommandText = "INSERT INTO phonebook(Id,uname,MobileNo) VALUES(@id,@uname,@MobileNo)"; 
       cmd.Parameters.AddWithValue("@Id", int.Parse(textBox1.Text)); 
       cmd.Parameters.AddWithValue("@uname", textBox2.Text); 
       cmd.Parameters.AddWithValue("@MobileNo", mobNo); 
       cmd.ExecuteNonQuery(); 
      } 
      catch (Exception) 
      { 
       throw; 
      } 
      finally 
      { 
       if (conn.State == ConnectionState.Open) 
       { 
        conn.Close(); 

       } 
      } 
     } 

    } 
} 

此代码正在为本地服务器数据库但是,当我试图与Web服务器数据库连接它给了以下错误。

MySql.Data.MySqlClient.MySqlException: Reading from the stream has failed. ---> System.IO.IOException: Unable to read data from the transport connection: A non-blocking socket operation could not be completed immediately. ---> System.Net.Sockets.SocketException: A non-blocking socket operation could not be completed immediately 
    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 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() 
    --- End of inner exception stack trace --- 
    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 mySqlConnectivity.Form1.button1_Click(Object sender, EventArgs e) in D:\VS\mySqlConnectivity\mySqlConnectivity\Form1.cs:line 35 
    at System.Windows.Forms.Control.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
    at System.Windows.Forms.Button.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 

我的IP地址和端口是正确的,因为使用这个我能够访问mysql服务器数据库。

任何机构都可以告诉我问题出在哪里,还有其他方式可以将C#应用程序与Web服务器数据库连接起来。

任何帮助将不胜感激。

谢谢

+0

您是否安装了'MYSQL Connector'来连接MYSQL数据库。 – Amit

+0

是的我已经安装了mysql-connector-net-6.7.4 – geeta

+0

@AmitAgrawal,你不需要安装它。如果您从项目中引用它,该库工作良好。 geeta,你是否在客户端/服务器上的防火墙中打开端口? – ps2goat

回答

1

从我看到(SERVER = xxx.xx.xxx.xx;端口= 80;)你试图连接到端口80(defalut HTTP端口)。默认的MySQL端口是3306,因此您应该更改端口以更正端口。

+1

我已经使用3306,但错误来了错误:无法连接到任何指定的MySQL主机。 – geeta

0

在.ini文件中设定以下值:

net_read_timeout=99999 

net_write_timeout=99999 

然后重新启动数据库服务器

这将工作。

+0

如何访问远程服务器上的.ini文件? –

1

您需要使用端口3306,现在您正在使用80,这是Web服务器正在监听的端口,而不是mysql服务器。

相关问题