Mysql connection returns a null value

도현 오 0 Reputation points
2023-02-19T14:39:24.7366667+00:00

I'm going to connect to mysql from c#.
However, when I try to connect to the internal ip address, conn returns NULL. The port is open and the connection string is not wrong. The root account has been given access from the outside and can also be accessed from the external OS. However, an error occurs when accessing from the c# program.
For your information, if you change the IP address to localhost, it will connect well.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;


namespace DB_conn
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ConnectionTest();
        }
        public static bool ConnectionTest()//db_conn
        {
            string connectString = string.Format("Server={0};Database={1};Uid ={2};Pwd={3};",
         "192.168.0.24", "my_site", "root", "security");
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connectString))
                {
                    conn.Open();
                    MessageBox.Show(string.Format("DB Access Complete."));

                }
                return true;
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
        }
    }

Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
986 questions
Developer technologies .NET Other
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. GeethaThatipatri-MSFT 29,542 Reputation points Microsoft Employee Moderator
    2023-02-23T14:03:58.3+00:00

    @도현 오 If it connects using the same code using localhost and not using the IP address, then it is more to do with permission/Grant issues or networking routing. But when you say you don’t see any exception or error and it is NULL then there is some conflict here. You should try printing the error message to check.  When you say you could access it from the external OS how did you access it?

    Regards

    Geetha

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.