【问题标题】:c# - System.FormatException: 'Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'c# - System.FormatException: 'Guid 应包含 32 位数字和 4 个破折号 (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。'
【发布时间】:2018-10-17 09:16:15
【问题描述】:

当我的数据库脱机时,我没有遇到此错误。我刚刚使用 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");

【问题讨论】:

  • 分享employee_id的类型。
  • 我强烈建议您不要在公共网站上发布您的数据库身份验证详细信息 ;-)
  • 另外,您的代码容易受到 SQL 注入的影响 - 您应该使用参数而不是连接字符串。见stackoverflow.com/questions/14376473/…
  • @Sham 我的employee_id 类型是int(4) 主键auto_increment zerofill
  • 有没有使用 GUID 的地方?

标签: c# mysql database


【解决方案1】:

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

我在 earlier StackOverflow post 的 cmets 中找到了解决方法

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

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

【讨论】:

  • 这对我有帮助!谢谢你。我在 db4free.net 上创建了一个免费的 MySQL 数据库,但遇到了这个晦涩的错误。将OldGuids=True; 添加到连接字符串是解决方法!
【解决方案2】:
  1. 鉴于您的错误发生在这里,您的问题发生在此之前: da.Fill(dt);
  2. 您有两个数据库字段和两个控件
  3. 因此,您的问题可能与两个数据库字段(用户名或密码)之一有关
  4. 我猜用户名是 varchar 什么的
  5. 因此您的密码很可能是 GUID

结论:
您可能应该将从 tbxPassword.Text 提取的值格式化为 GUID。
而且,如上所述,您还需要防止 SQL 注入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    相关资源
    最近更新 更多