dbhlpe.conn.Open();
            string sql = "select * from userId where famName=@5";
            SqlCommand cmd = new SqlCommand(sql, dbhlpe.conn);
            cmd.Parameters.Add("@5", SqlDbType.VarChar).Value = textBox1.Text;
            int count = (int)cmd.ExecuteScalar(); 
            if (count > 0)
            {
                MessageBox.Show("帐户已存在,请重新选取帐号");
                textBox1.Text = "";
                textBox1.Focus();
                button1.Enabled = false;
            }
            else
            {
                MessageBox.Show("当前帐户可用");
                button1.Enabled = true;
            }
输入已经存在的数据,还是弹出对话框来说帐户已经存在,但是输入没有存在的数据就报错
 int count = (int)cmd.ExecuteScalar(); 
          未将对象引用设置到对象的实例。请帮忙看看

解决方案 »

  1.   

     "select count(*) from userId where famName=@5"; 
    判断 结果是0 还是1 
      

  2.   

    dbhlpe.conn.Open(); 
                string sql = "select count(*) from userId where famName=@5"; 
                SqlCommand cmd = new SqlCommand(sql, dbhlpe.conn); 
                cmd.Parameters.Add("@5", SqlDbType.VarChar).Value = textBox1.Text; 
                int count = (int)cmd.ExecuteScalar(); 
                if (count > 0) 
                { 
                    MessageBox.Show("帐户已存在,请重新选取帐号"); 
                    textBox1.Text = ""; 
                    textBox1.Focus(); 
                    button1.Enabled = false; 
                } 
                else 
                { 
                    MessageBox.Show("当前帐户可用"); 
                    button1.Enabled = true; 
                } 
      

  3.   

    冒昧的问一句,用SQL2005写的数据库文件 能不能本地连接?就是不用装SQL2005也可以增删改查?