繁体   English   中英

将 c#.net 连接到 phpmyadmin 数据库(MysqL)

[英]Connecting c#.net To phpmyadmin database(MysqL)

我有一个用 C# 编写的 .NET 程序,它连接到我网站的 cpanel 中的 phpmyadmin。 我的问题是,当我在办公室时,我可以连接到我的 phpmyadmin 服务器,但是当我在家里时,我无法连接到服务器。

我已经在 cpanel 的 remoteMySQL 中包含了我的 ISP (www.whatismyip.com/www.speedtest.net) IP,但由于某种原因,它只能在办公室使用。 您认为这可能是因为他们是我要连接的网站的主机吗?

我的连接是这样的:

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

请注意,这种连接方式仅适用于办公室 :(

首先,您要连接到数据库服务器,即 MySQL 而不是 PHPMyAdmin,后者是 MySQL 的 Web 客户端。

其次,假设您所说的是正确的,并且您实际上已经允许您的 IP 使用提供的登录名登录到 MySQL 数据库,您仍然需要确保您的公司允许远程 MySQL 连接(如果他们重视安全性,则不太可能) .

    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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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