WinForm连接数据库,语句执行查不到结果,但连接和命令执行没出错,查询语句复制到数据库查询可查到结果,是怎么回事,求大神指点。

解决方案 »

  1.   

    代码如下  public static DataTable GetDataTable(string sql)
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = sql;
                    cmd.Connection = Connection;
                    cmd.CommandTimeout = 100;
                    DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(ds);
                    if (ds != null && ds.Tables.Count>0)
                    {
                        return ds.Tables[0];
                    }
                    else
                    {
                        return null;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("数据处理异常!");
                    return null;
                }
                finally
                {
                    Close();
                }
            }
      

  2.   


    我单步调了下,没任何问题,就是执行   SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(ds);之后,if (ds != null && ds.Tables.Count>0)这一句判断发现ds中并没有表,将执行的命令语句复制到数据库环境下查询可以查到数据
      

  3.   

    DataTable GetDataTable(string sql)
    你要返回DataTable,直接用DataTable table =new DataTable(); 
    da.Fill(table);
    return table;
    没有必要用DataSet,你这不是脱裤子放屁?
      

  4.   


    关键的问题不在这 在于我查不到数据,而不是赋值时丢掉数据,我执行  SqlCommand cmd = new SqlCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = sql;
                    cmd.Connection = Connection;
                    cmd.CommandTimeout = 100;
                    return cmd.ExecuteReader();
    也没结果
      

  5.   

       cmd.CommandTimeout = 100;会不会数据太多,超时了。
      

  6.   

      cmd.Connection = Connection;
    你的Connection在哪?这里好象没有呀
      

  7.   


    sql语句完全正常,因为直接copy到数据库中查询时结果如下
      

  8.   


    本来新建的是sqlcommand默认CommandTimeout是30ms,结果查不出来,我担心是因为超时,所以把执行时间加长到了100ms,结果还没效果,说明不是这个原因
      

  9.   

     public static DataTable GetDataTable(string SqlString)
            {
                SqlDataAdapter Adapter = new SqlDataAdapter();
                DataTable table = new DataTable();
                using (SqlConnection connetion = new SqlConnection(connectionString))
                {
                    Adapter.SelectCommand = new SqlCommand(SqlString, connetion);
                    Adapter.Fill(table);
                    Adapter.Dispose();
                    return table;
                }
            }
    你改connectionString一下用这个看