【问题标题】:MySQL Connection with Visual C# Express 2008与 Visual C# Express 2008 的 MySQL 连接
【发布时间】:2023-04-07 15:24:02
【问题描述】:

好的,所以基本上我已经在 PhpMyAdmin 上制作了 MySQL 表。它在本地主机上,用户名 root,没有密码。

我正在开发基于 c# on visual express 2008 的 Windows 应用程序。我有以下代码用于保存/加载来自 MySQL 的数据的按钮(我已经按照一些链接/tuts 来达到这一点,但是不知道这在理论上如何连接到 MySQL @ phpmyadmin,我的意思是我不需要从 PhpmyAdmin 数据库下载文件并引用或将其作为插件添加到脚本中吗?完全迷失在这里..):

        String connString = "SERVER = localhost; DATABASE = request; User ID = root; ID =; UserName =; Date =; Type =; Rules =;"; 
        MySqlConnection mcon = new MySqlConnection(connString);
        String command = "SELECT * FROM requesttcw";
        MySqlCommand cmd = new MySqlCommand(command, mcon);
        MySqlDataReader reader;

        try
        {
            mcon.Open();
            cmd.ExecuteNonQuery();
            reader = cmd.ExecuteReader();
            cmd.CommandType = System.Data.CommandType.Text;
            while (reader.Read() != false)
            {
                Console.WriteLine(reader["ID"]);
                Console.WriteLine(reader["ClanName"]);
                Console.WriteLine(reader["Date"]);
                Console.WriteLine(reader["Type"]);
                Console.WriteLine(reader["Rules"]);

            }

            Console.ReadLine(); 

        }
        catch (Exception)
        {
            MessageBox.Show("ERROR: There was an error trying to connect to the DB!");
            return;
        }
        cmd.CommandText = "INSERT INTO requesttcw (ClanName, Date, Type, Rules) VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "', '" + textBox3.Text + "', '" + richTextBox1.Text + "' LIMIT 1)";

        try
        {
            cmd.ExecuteNonQuery();
            MessageBox.Show("You're Request Has Been Posted!");
        }
        catch (Exception ex)
        {
            string message = ("ERROR: There was an error submitting your form!" + ex + "");
            DialogResult result = MessageBox.Show(message, "ERROR", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);

            switch (result)
            {
                case DialogResult.Retry:
                    Application.Restart();
                    break;
                case DialogResult.Cancel:
                    this.Close();
                    break;
            }
        }

当我运行它时,输入我的数据,然后单击按钮,它在线上给我这个错误(MySqlConnection mcon = new MySqlConnection(connString); * 不支持关键字。 参数名称:id *

请告诉我如何将它完全连接到 MySQL。我还下载了 MySQL 连接器并引用了 mysql.data.dll 文件。所以那部分也完成了......

【问题讨论】:

    标签: c# mysql phpmyadmin


    【解决方案1】:

    试试下面的连接字符串:

    string connString = "Server=localhost;Database=request;Uid=root;Pwd=;";
    

    还可以尝试清理您的代码,并通过将它们包装在using 语句中来确保您正确地处理IDispoable 资源,例如SQL 连接和命令。还要确保您使用的是准备好的语句,否则您的代码容易受到 SQL 注入攻击,并且您的数据库可能会很快被破坏(眨眼间):

    string connString = "Server=localhost;Database=request;Uid=root;Pwd=;";
    
    using (MySqlConnection mcon = new MySqlConnection(connString))
    using (MySqlCommand cmd = mcon.CreateCommand())
    {
        mcon.Open();
        cmd.CommandText = "SELECT * FROM requesttcw";
        using (MySqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                Console.WriteLine(reader["ID"]);
                Console.WriteLine(reader["ClanName"]);
                Console.WriteLine(reader["Date"]);
                Console.WriteLine(reader["Type"]);
                Console.WriteLine(reader["Rules"]);
            }
        }
    }
    
    using (MySqlConnection mcon = new MySqlConnection(connString))
    using (MySqlCommand cmd = mcon.CreateCommand())
    {
        mcon.Open();
        cmd.CommandText = "INSERT INTO requesttcw (ClanName, Date, Type, Rules) VALUES (@ClanName, @Date, @Type, @Rules)";
        cmd.Parameters.AddWithValue("@ClanName", textBox1.Text);
    
        // Warning if the Date column in your database is of type DateTime
        // you need to parse the value first, like this:
        // cmd.Parameters.AddWithValue("@Date", Date.Parse(textBox2.Text));
        cmd.Parameters.AddWithValue("@Date", textBox2.Text);
        cmd.Parameters.AddWithValue("@Type", textBox3.Text);
        cmd.Parameters.AddWithValue("@Rules", richTextBox1.Text);
    
        cmd.ExecuteNonQuery();
    }
    

    就异常处理而言,我在示例中省略了它,但您显然可以将 using 语句包装在 try/catch 块中。

    【讨论】:

    • 达林·迪米特洛夫感谢人!这解决了一切!!!!我花了一天的时间试图解决这个问题,但你只是帮我解决了这个问题!! :D
    • @DamageDz,太好了,我很高兴能帮上忙。如果这篇文章对您有帮助,请点击它旁边的勾来考虑marking it as answer
    • @vucetica,ADO.NET 管理一个连接池。所以第二个using不会打开到数据库的新连接。它只是从connection pool 中提取现有连接。在 using 语句结束时,连接并没有关闭,而是返回到连接池以便重用。就您的第二句话而言,我更喜欢在我的应用程序中分离关注点。这两个通常会进入 2 个完全独立的方法,以后可以重用。
    • 池化将起作用,您可以创建新命令,但实际上不需要额外的代码,除非这是您的偏好。
    【解决方案2】:

    connString 变量中,ID =;User ID = root 之后。

    【讨论】:

      【解决方案3】:

      您的连接字符串指定您的应用程序和 MySQL 服务器之间的连接。 您得到的错误是,因为您在连接字符串中犯了错误。 我认为以下内容会更好地满足您的需求:

      Server=localhost;Database=request;Uid=myUsername;Pwd=myPassword;
      

      【讨论】:

        猜你喜欢
        • 2010-10-28
        • 2011-01-07
        • 1970-01-01
        • 2010-11-17
        • 1970-01-01
        • 1970-01-01
        • 2011-09-09
        • 2011-09-19
        • 1970-01-01
        相关资源
        最近更新 更多