简体   繁体   中英

Unable to connect remote server mysql database from C# application

I am trying to connect my C# application to web server database(mysql). I am using the following code.

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();

                }
            }
        }

    }
}

This code is working for local server database But when I am trying to connect with web server database it is giving the following error.

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)

My IP address and Port is correct because using this I am able to access mysql server database.

Can any body please tell me where is the problem and it there any other way to connect C# application with web server database.

Any help will be appreciated.

Thank you

From what I see (SERVER=xxx.xx.xxx.xx;Port=80;) you are trying to connect to port 80 (defalut http port). The defalut MySQL port is 3306, so you should change the port to correct one.

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

In .ini file set following values:

net_read_timeout=99999 

net_write_timeout=99999 

Then restarting db server

This will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM