【问题标题】:I understand how to connect web mysql database with C# windows form application我了解如何将 web mysql 数据库与 C# windows 窗体应用程序连接起来
【发布时间】:2020-04-22 13:13:59
【问题描述】:

昨天,我花了很多时间搜索连接Mysql WEB databaseC# windows form application 的教程,我尝试了很多代码并观看了大约 10 个类似的教程但没有任何帮助,我的意思是它对我没有帮助,因为它没有连接类型我需要...

我想连接这个数据库: Databsae image by lightshot

在这段代码中:

using MySql.Data.MySqlClient;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace CS_Enginer
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }


        private void textBox1_Enter(object sender, EventArgs e)
        {
            if (textBox1.Text == "Username")
            {
                textBox1.Text = "";
                textBox1.ForeColor = Color.DimGray;
            }

        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                textBox1.Text = "Username";
                textBox1.ForeColor = Color.DimGray;
            }
        }

        private void textBox2_Enter(object sender, EventArgs e)
        {
            if (textBox2.Text == "Password")
            {
                textBox2.Text = "";
                textBox2.ForeColor = Color.DimGray;
                textBox2.UseSystemPasswordChar = true;
            }

        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            if (textBox2.Text == "")
            {
                textBox2.Text = "Password";
                textBox2.ForeColor = Color.DimGray;
                textBox2.UseSystemPasswordChar = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void login_MouseDown(object sender, MouseEventArgs e)
        {
        }

        private void login_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }
    }
}

我知道我不应该发布图片,但我不知道如何解释...对此感到抱歉,提前致谢!!!

【问题讨论】:

  • 我在 c# windows 窗体应用程序中创建登录系统,我想连接网络数据库以获取我发送的图像..
  • 看病现在给你发图片,等一下 :) prnt.sc/s3zosr prnt.sc/s3zp5r
  • 我说我看了很多教程,但我不明白,我试过的一切都不起作用......

标签: c# mysql database


【解决方案1】:

注意:仅当从网站的 CPANEL 启用到数据库的远程连接时,以下操作才有效。

要连接到数据库,您需要一个连接字符串和库来帮助您连接并从数据库中检索数据。对于 MySql,您可以使用此处提供的 MySql 连接器 - MySql connector

下载后,右键单击您的项目名称并选择“添加”,然后选择“引用”,将连接器的引用添加到您的项目。

添加引用后,导入项目using MySql.Data.MySqlClient;中的类。

现在,您需要形成连接字符串并打开连接,如下所示 -

private void MySqlInteraction()
    {
        string connString = null;
        MySqlConnection mySqlConn;
        // It would be better if the below connection string is provided by the vendor
        // of the database website that you are trying to access as you might not have
        // all the required information
        connString = "server=youronlineserver;database=dbname;uid=root;pwd=abc123;";
        mySqlConn = new MySqlConnection(connString);
        try
        {
            mySqlConn.Open();
            MessageBox.Show ("Connection Open ! ");
            //enter your db querying logic here
            mySqlConn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Can not open connection ! ");
        }
    }

【讨论】:

  • 您是否确保允许远程连接到数据库?
  • 我去检查一下,等一下
  • 无法在此网站上启用远程连接,因为它是共享网站..
  • 在这种情况下,我认为您将无法访问远程服务器。您仍然可以尝试的一件事是,如果服务器上有某种 IP 白名单,您可以在其中授予对您 IP 的访问权限。让我知道是否需要其他任何东西。另外,如果答案对您有帮助,请将其标记为已接受。
猜你喜欢
  • 2013-08-03
  • 2012-02-10
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 2022-06-14
  • 1970-01-01
  • 2013-05-12
相关资源
最近更新 更多