当前位置:编程学习 > C#/ASP.NET >>

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

WinForm连接数据库,语句执行查不到结果,但连接和命令执行没出错,查询语句复制到数据库查询可查到结果,是怎么回事,求大神指点。 --------------------编程问答-------------------- 看具体代码,可能某个变量赋值不正确 --------------------编程问答-------------------- 没有代码怎么知道你的代码没有错? --------------------编程问答-------------------- 发代码看看,顶一下 --------------------编程问答-------------------- 代码如下  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();
            }
        } --------------------编程问答-------------------- 楼主调试一下,看看是哪个地方没数据吧。 --------------------编程问答--------------------
引用 5 楼 hengwang007 的回复:
楼主调试一下,看看是哪个地方没数据吧。


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

DataTable table =new DataTable(); 
da.Fill(table);
return table;
没有必要用DataSet,你这不是脱裤子放屁? --------------------编程问答--------------------
引用 7 楼 DENQH 的回复:
DataTable GetDataTable(string sql)
你要返回DataTable,直接用

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


关键的问题不在这 在于我查不到数据,而不是赋值时丢掉数据,我执行  SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                cmd.Connection = Connection;
                cmd.CommandTimeout = 100;
                return cmd.ExecuteReader();
也没结果 --------------------编程问答--------------------    cmd.CommandTimeout = 100;会不会数据太多,超时了。 --------------------编程问答--------------------   cmd.Connection = Connection;
你的Connection在哪?这里好象没有呀 --------------------编程问答--------------------
引用 10 楼 DENQH 的回复:
cmd.Connection = Connection;
你的Connection在哪?这里好象没有呀


--------------------编程问答-------------------- sql语句? --------------------编程问答--------------------
引用 12 楼 DENQH 的回复:
sql语句?


sql语句完全正常,因为直接copy到数据库中查询时结果如下 --------------------编程问答--------------------
引用 9 楼 zlyang19860701 的回复:
cmd.CommandTimeout = 100;会不会数据太多,超时了。


本来新建的是sqlcommand默认CommandTimeout是30ms,结果查不出来,我担心是因为超时,所以把执行时间加长到了100ms,结果还没效果,说明不是这个原因 --------------------编程问答--------------------
 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一下用这个看 --------------------编程问答--------------------
引用 15 楼 DENQH 的回复:
C# code?123456789101112 public static DataTable GetDataTable(string SqlString)        {            SqlDataAdapter Adapter = new SqlDataAdapter();            DataTable table = new DataTabl……
  还是不行,这个问题之前从没出现过,从上周六下午出现了就一直出现,郁闷死了
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,