string connStr = "Data Source=HM;Initial Catalog=MyDB;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            string sql = "select Name from Customer where (CustomerID = 'Hm')";
            SqlCommand m_Command = new SqlCommand(sql, conn);
            //CommandType m_CommandType = CommandType.Text;
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(m_Command);
            da.Fill(ds);
            this.textBox1.Text=ds.Tables[0].DefaultView[0].ToString();
            
            conn.Close();提示:
this.textBox1.Text=ds.Tables[0].DefaultView[0].ToString();
有问题:索引 0 不是为负数,就是大于行数。
应该怎么写????????????????

解决方案 »

  1.   


    this.textBox1.Text=ds.Tables[0].Rows[0][0].ToString(); 这里获得的是结果集里面的第1张表的第一行的第一个字段
      

  2.   

    this.textBox1.Text=ds.Tables[0].DefaultView[0].ToString(); 
    --------------------------
    DefaultView后面不需要索引。位置0处没有任何行 
    --------------------
    没有记录。从查询分析器验证是否确实有数据。
      

  3.   

    他应该是只那个Tables后面的索引为什么啊 。
    要不哪个大哥,写个实例出来,看看怎么实现连接sqlserver,然后显示到textbox里面的?
      

  4.   

    string sql = "select Name from Customer where (CustomerID = 'Hm')"; 
    string sql = "select Name from Customer where CustomerID = 'Hm'"; 
      

  5.   

    如果是Tables后面的索引的问题,验证一下确实有对应的数据库、表、记录。打开查询分析器,选择数据库,把你的SQL语句复制过去,看一下执行结果。
      

  6.   


    我要崩溃了。原来是hm写错了,hm前面在表里还有个空格,怪不得查其他的行呢。
    崩溃啊