Вопрос

I have .NET program written in C# that connects to my website's phpmyadmin which is in cpanel. My problem is that when I'm at the office I can connect to my phpmyadmin server, but when i'm, for example in my house, I can not connect to the server.

I've already included my ISP (www.whatismyip.com/www.speedtest.net) IP in my remoteMySQL in cpanel, but for some reason it only works at the office. Do you think that maybe it is because they are the one hosting the website that I'm connecting to?

My Connection is something like this:

string connection = "SERVER=MyWebsite.com; Database=databse_pcc; Uid=mywebuser; Pwd=password;";

        con9.Open();
        MySqlCommand cmd9 = new MySqlCommand("Select * from biometrics_tbl LIMIT 1", con9);
        cmd9.CommandType = CommandType.Text;
        cmd9.Connection = con9;
        MySqlDataReader rdr9 = cmd9.ExecuteReader();
        while (rdr9.Read())
        {
            MessageBox.Show(rdr9.GetString(2));
        }
        con9.Close();

Note this style of connection is working only at the office :(

Это было полезно?

Решение

First off, you're connecting to a database server, namely MySQL not PHPMyAdmin which is a web client for MySQL.

Second assuming what you've said is correct and you have in fact allowed your IP to login to the MySQL database using the login provided, you'll still need to make sure your company allows remote MySQL connections (not likely if they value security).

Другие советы

    using MySql.Data.MySqlClient;
    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;
    
    namespace WindowsFormsApp3
    {
        public partial class Form1: Form
        {
            MySqlConnection conn = new MySqlConnection(@"Data Source = localhost;port=3306;Initial Catalog=loginandregister; User Id=root;password=''");
    
            public Form1()
            {
                InitializeComponent();
            }
            bool _regiter()
            {
                int retval = 0;
                try
                {
                    string SQLS = string.Empty;
                    SQLS += "INSERT tb_user SET ";
                    SQLS += "fullname='" + textBox1.Text + "'";
                    SQLS += ",user='" + textBox2.Text + "'";
                    SQLS += ",password='" + textBox3.Text + "'";
    
    
                    conn.Open();
                    using (MySqlCommand com = new MySqlCommand(SQLS, conn))
                    {
                        retval = com.ExecuteNonQuery();
    
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
    
                }
                finally { conn.Close(); }
                return retval > 0;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                _regiter();
                MessageBox.Show("Register Success");
                Form2 oRegisterrr = new Form2();
                oRegisterrr.ShowDialog();
                this.Close();
            }
        }
    }

    using MySql.Data.MySqlClient;
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;

namespace WindowsFormsApp3
{
    public partial class Form2 : Form
    {
        MySqlConnection conn = new MySqlConnection(@"Data Source = localhost;port=3306;Initial Catalog=loginandregister; User Id=root;password=''");

        public Form2()
        {
            InitializeComponent();
        }
        private void LoginProcess()
        {
            if (login(textBox1.Text, textBox2.Text))
            {
                MessageBox.Show("Welcome " + textBox1.Text);
                this.Hide();
                //Form_Menu oFormMenu = new Form_Menu();
               // oFormMenu.ShowDialog();
                this.Close();
            }
            else
            {
                MessageBox.Show("Login Fail");
            }
        }
        private Boolean login(string sUsername, string sPassword)
        {
            string SQL = "SELECT user,password FROM tb_user";
            conn.Open();
            MySqlCommand cmd = new MySqlCommand(SQL, conn);
            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if ((sUsername == reader.GetString(0)) && (sPassword == reader.GetString(1)))
                {
                    conn.Close();
                    return true;
                }
            }
            conn.Close();
            return false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LoginProcess();
        }
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top