首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将c#.net连接到phpmyadmin数据库(MysqL)

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

Stack Overflow用户
提问于 2014-05-13 13:09:33
回答 2查看 3.7K关注 0票数 1

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

我已经在cpanel中的remoteMySQL中包含了我的ISP (www.whatismyip.com/www.veltest.net) IP,但出于某种原因,它只在办公室工作。你认为可能是因为他们是我所连接的网站的主机吗?

我的联系是这样的:

代码语言:javascript
复制
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();

注意,这种连接方式只在办公室工作:(

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-13 13:21:13

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

第二,假设您所说的是正确的,并且实际上允许您的IP使用所提供的登录登录到MySQL数据库,那么您仍然需要确保您的公司允许远程MySQL连接(如果它们重视安全性的话不太可能)。

票数 0
EN

Stack Overflow用户

发布于 2021-03-23 09:04:01

代码语言:javascript
复制
    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();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23632208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档