【问题标题】:MySql.Data.MySqlClient.MySqlException(0x80004005) you have an error in your SQL syntaxMySql.Data.MySqlClient.MySqlException(0x80004005) 你的 SQL 语法有错误
【发布时间】:2020-03-25 21:50:36
【问题描述】:

当我尝试使用 SQL 和 Visual Studio 2019 创建连接时出现此错误。

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

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

        private void BtnConnection_Click(object sender, EventArgs e)
        {
            string servidor = TXTServer.Text;
            string puerto = TXTPort.Text;
            string usuario = TXTUser.Text;
            string contraseña = TXTPassword.Text;
            string dato = "";

            string cadenaConexion = "server=" + servidor + "; port=" + puerto + "; user id=" + usuario + "; password=" + contraseña + "; database=mysql;";

            MySqlConnection conexionBD = new MySqlConnection(cadenaConexion); 

            try    
            {                 
                conexionBD.Open();

                MySql.Data.MySqlClient.MySqlDataReader reader = null;

                MySqlCommand cmd = new MySqlCommand("Mostrar database", conexionBD);
                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    dato += reader.GetString(0) + "\n";    
                }
            }
            catch(MySqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            MessageBox.Show(dato);
        }
    }
}

如果有人可以帮助我

【问题讨论】:

  • 你希望 MySQL 对作为 SQL 命令传递的"Mostrar database" 做什么?建议的学习材料:Online SQL Tutorial 此外,c# 和 C 和 c++ 在这方面是不可互换的。注意你的标签
  • 那么,MySqlCommand cmd = new MySqlCommand("Mostrar database", conexionBD); 是做什么的?
  • 和往常一样,MySqlCommand的第一个参数应该是测试mysql代码或者存储过程名称。根据您的代码,您想从代码中得到什么?

标签: c# mysql visual-studio


【解决方案1】:

使用

MySqlCommand cmd = new MySqlCommand("SHOW DATABASES;", conexionBD);

获取存储在服务器中的所有数据库;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 2023-02-24
    • 2012-03-13
    • 1970-01-01
    相关资源
    最近更新 更多