伙计们,我正在将我的表单与 mysql 数据库连接 我想检查文本框文本是否存在于数据库中我编写了代码,但即使文本存在于数据库中也显示错误
这是我的代码:
MySqlConnection con = new MySqlConnection(connectionString);
i = 0;
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from informat where u_ser = '" + metroTextBox2.Text + "' ";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
i = dt.Rows.Count;
int userial = metroTextBox2.Text.Length;
if (userial <= 5 || metroTextBox2.Text != null)
{
MessageBox.Show("error");
}
else {
test frm = new test();
this.Hide();
frm.Show();
}
最佳答案
如果文本框包含输入的文本,则以下条件将计算为 true。这就是为什么您总是会在对话框中看到错误消息。
if (userial <= 5 || metroTextBox2.Text != null)
{
MessageBox.Show("error");
}
您是否打算在上述情况下使用 AND 运算符,请检查它。
关于c# - 检查 MySql DB 中是否存在文本框文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45100063/