读书人

数据库查询主码存在也罢

发布时间: 2012-12-14 10:33:08 作者: rapoo

数据库查询主码存在与否
如果存在主码则弹框,不存在则继续。

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.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();
}
}
[其他解释]
对。是这样比较的。。。。。。。。。。

读书人网 >C#

热点推荐