首页 > 解决方案 > c# - System.FormatException: 'Guid 应包含 32 位数字和 4 个破折号 (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。'

问题描述

当我的数据库脱机时,我没有遇到此错误。我刚刚使用 db4free.net 使我的数据库联机。

每次我登录都会出现这个错误。有人可以指出什么问题吗?

private void btnLogIn_Click(object sender, EventArgs e)
    {
        string query = "select * from tbl_accounts where username='" + tbxUsername.Text + "' and password='" + tbxPassword.Text + "'";
        MySqlCommand command = new MySqlCommand(query, connection);
        MySqlDataAdapter da = new MySqlDataAdapter(command);
        DataTable dt = new DataTable();
        da.Fill(dt);

        try
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];

                if (dt.Rows.Count > 0)
                {
                    employee_id = (dr["employee_id"].ToString().PadLeft(4, '0'));
                    fullname = (dr["account_firstname"] + " " + dr["account_lastname"]).ToString();
                    this.Invoke(new Action(() =>
                    {
                        connection.Open();
                        command.ExecuteNonQuery();
                        connection.Close();
                        this.Close();
                        th = new Thread(openNewForm);
                        th.SetApartmentState(ApartmentState.STA);
                        th.Start();
                    }));
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

这是错误:

异常截图

更新:这是我的连接字符串:

MySqlConnection connection = new MySqlConnection("datasource=db4free.net;Convert Zero Datetime=True;SslMode=none");

标签: c#mysqldatabase

解决方案


距 OP 日期几乎整整 1 年,我在尝试使用 NuGet 包 MySQL.Data 时遇到了同样的错误

我在早期的 StackOverflow 帖子的评论中找到了解决方法

基本上,您可以通过添加OldGuids=True;作为@noontz 提到的 MySQL DB 连接字符串的一部分来避免错误。

但是,正如@BradleyGrainger 所指出的,请注意,通过这样做,任何BINARY(16)列都将返回为 aGuid而不是byte[].


推荐阅读