数据库查询主码存在与否
如果存在主码则弹框,不存在则继续。
SqlCommand one = new SqlCommand("select version from t_version_base where version='" + textBox2.ToString() + "'", conn);
one.CommandType = CommandType.Text;
one.ExecuteNonQuery();
SqlDataReader dr = null;
dr = one.ExecuteReader();
while (dr.Read())
{
if (dr["version"] != null)
{
string str = dr["version"].ToString();
if (str.ToString() == textBox2.ToString())
{
MessageBox.Show("hhhhhhhhhhhhhhhhhhh");
} dr.Close();
}
}
testbox2.tostring 是输入的主码,怎样读取数据库查询结果并比较?
------最佳解决方案--------------------SqlCommand one = new SqlCommand("select count(1) from t_version_base where version='" + textBox2.ToString() + "'", conn);
int count = (int)one.ExecuteScalar();
if(count > 0){
MessageBox.Show("hhhhhhhhhhhhhhhhhhh");
}else{
// continue;
}
------其他解决方案--------------------SqlCommand one = new SqlCommand("select version from t_version_base where version='" + textBox2.ToString() + "'", conn);
one.CommandType = CommandType.Text;
one.ExecuteNonQuery();//这句是多余的
SqlDataReader dr = null;
dr = one.Execute