首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

数据库查询主码存在也罢

2012-12-14 
数据库查询主码存在与否如果存在主码则弹框,不存在则继续。SqlCommand one new SqlCommand(select versi

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

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

热点排行