如果存在主码则弹框,不存在则继续。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 是输入的主码,怎样读取数据库查询结果并比较?

解决方案 »

  1.   

    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;
    }
      

  2.   

    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();
                            //str本来就是string,不同ToString()了,取文本框的值用Text属性
                            if (str.ToString() == textBox2.Text)
                            {
                                MessageBox.Show("hhhhhhhhhhhhhhhhhhh");
                            }                        dr.Close();
                        }
                    }