程序出错的代码:红色的那一段
string ConStr = @"Data Source=PC-201302281918\SQLEXPRESS;Initial Catalog=DataServer;Integrated Security=True";
            SqlConnection con = new SqlConnection(ConStr);
            string SqlStr = "select spendProcd as 消费项目,spendMoney as 消费金额,spendDate as 消费日期 from Consume";
            SqlDataAdapter adaa = new SqlDataAdapter(SqlStr, con);
            DataSet ds = new DataSet();
            adaa.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
求助各位大神啊,实在是找不出是哪里出现了问题。异常

解决方案 »

  1.   

    SqlConnection 没有open。
    ps:错误在红色区域??
      

  2.   

    嗯,在SqlConnection con = new SqlConnection(ConStr);之后,你应该Open一下,打开连接。con.Open();
    然后用完了别忘了关上,con.Close();不想手动关的话就Using一下   using(SqlConnection con = new SqlConnection(ConStr))
    {
    这里写那些SqlDataAdapter 那些东西;
    }
      

  3.   

    open需要。
    把sql里面的汉字去掉试试看呢,你这个错要么ConStr有问题,要么sqlStr有问题。
      

  4.   

    如果不是open的影响的话,还真是不可思议,楼主引用了system.data了吧?
      

  5.   

    我用了system.data,这是我修改后的代码,
                string ConStr = @"Data Source=PC-201302281918\SQLEXPRESS;Initial Catalog=DataServer;Integrated Security=True";
                SqlConnection con = new SqlConnection(ConStr);
                con.Open();
                string SqlStr = "select spendProcd as 消费项目,spendMoney as 消费金额,spendDate as 消费日期 from Consume";
                SqlDataAdapter adaa = new SqlDataAdapter(SqlStr, con);
                DataSet ds = new DataSet();
                adaa.Fill(ds);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
                con.Close();
    可是还是出现那样的问题,肿么办?