c#-system.formatexception:'guid应包含32位数字和4个破折号(xx-x-x-x-x)'

p5cysglq  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(306)

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

rur96b6h1#

如果你的错误发生在这里,你的问题就发生在这之前: da.Fill(dt); 有两个数据库字段和两个控件
因此,您的问题可能与两个数据库字段(用户名或密码)中的一个有关
我猜用户名是varchar之类的
因此,您的密码可能是一个guid
结论:
您可能应该格式化从 tbxPassword.Text 作为guid。
而且,如上所述,您还需要防止sql注入。

qnzebej0

qnzebej02#

几乎整整一年前,我在尝试nuget包mysql.data时遇到了相同的错误
我在前面的stackoverflow帖子的评论中找到了解决方法
基本上,您可以通过添加 OldGuids=True; 作为@noontz提到的mysql db连接字符串的一部分。
然而,正如@bradleygrainger所指出的,要知道这样做,任何 BINARY(16) 列将作为 Guid 而不是 byte[] .

相关问题