WinForm连接数据库,语句执行查不到结果,但连接和命令执行没出错,查询语句复制到数据库查询可查到结果,是怎么回事,求大神指点。 WinForm连接数据库,语句执行查不到结果,但连接和命令执行没出错,查询语句复制到数据库查询可查到结果,是怎么回事,求大神指点。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 代码如下 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(); } } 我单步调了下,没任何问题,就是执行 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,你这不是脱裤子放屁? 关键的问题不在这 在于我查不到数据,而不是赋值时丢掉数据,我执行 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在哪?这里好象没有呀 sql语句完全正常,因为直接copy到数据库中查询时结果如下 本来新建的是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一下用这个看 C#中string.contains(string str)忽略大小写 求一个url的正则表达式 C#判断文件是否已经打开 C#字符串格式化问题 C#程序实现远程开关机? 软件升级问题的研究 struct在 c#中如何定义 判断文件是否被占用? 24.6 + 12.3 不等于36.9! double 运算引发的的错误 有谁用过Xxmm这个数据层的组件? 想实现当用户输入一串字母后弹出一个窗口,用于后台配置,用什么方法检测用户任意时刻输入一串字母 itextsharp 合并成一个横纵交替的pdf文件
{
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();
}
}
我单步调了下,没任何问题,就是执行 SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);之后,if (ds != null && ds.Tables.Count>0)这一句判断发现ds中并没有表,将执行的命令语句复制到数据库环境下查询可以查到数据
你要返回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();
也没结果
你的Connection在哪?这里好象没有呀
sql语句完全正常,因为直接copy到数据库中查询时结果如下
本来新建的是sqlcommand默认CommandTimeout是30ms,结果查不出来,我担心是因为超时,所以把执行时间加长到了100ms,结果还没效果,说明不是这个原因
{
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一下用这个看