没有获取ds中的数据 [英] not getting the data in ds

查看:87
本文介绍了没有获取ds中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Heloo guys!

我可能知道以下代码有什么问题。如果我使用以下代码,我无法获取数据集中的数据。



谢谢

<前lang =c#> 字符串 Bagquery;

尝试
{
string dtfrom = < span class =code-keyword> this .datefrom.Value.ToShortDateString();
string dateto = this .Dateto.Value.ToShortDateString();
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
DataConnection data = new DataConnection();
con = data.GetConnection();
clsBagData bagdata = new clsBagData();
Bagquery = bagdata.Bagdata(dtfrom,dateto,txtFname.Text,txtSName.Text,cmbEmailID.Text,CmbSubmode.Text);
con.Open();
ds = new DataSet();
da = new SqlDataAdapter(Bagquery,con);
da.SelectCommand.Parameters.Add( @ dtfrom,SqlDbType.VarChar,< span class =code-digit> 11 );
da.SelectCommand.Parameters [ @ dtfrom]。Value = dtfrom;
da.SelectCommand.Parameters.Add( @ dtto,SqlDbType.VarChar,< span class =code-digit> 11 );
da.SelectCommand.Parameters [ @ dtto]。Value = dateto;
da.SelectCommand.Parameters.Add( @ RFname,SqlDbType.VarChar,< span class =code-digit> 11 );
da.SelectCommand.Parameters [ @ RFname]。Value = txtFname.Text;
da.SelectCommand.Parameters.Add( @ RSurname,SqlDbType.VarChar,< span class =code-digit> 11 );
da.SelectCommand.Parameters [ @ RSurname]。Value = txtSName.Text;
da.SelectCommand.Parameters.Add( @ cmbemailid,SqlDbType.VarChar,< span class =code-digit> 11 );
da.SelectCommand.Parameters [ @ cmbemailid]。Value = cmbEmailID.Text;
da.SelectCommand.Parameters.Add( @ cmbmode,SqlDbType.VarChar,< span class =code-digit> 11 );
da.SelectCommand.Parameters [ @ cmbmode]。Value = CmbSubmode.Text;
da.Fill(ds);
dataGridView2.DataSource = ds.Tables [ 0 ];
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

解决方案

我们不能从这段代码中给你一个绝对的答案:它错过了很远,也是许多。例如,您的查询本身是使用您不显示的方法bagdata.Bagdata汇编的,并且由于查询字符串可能对您无法获得所需数据的原因相当重要,因此需要看一下。



所以要做的第一件事就是使用调试器断开这一行:

 con.Open(); 

并检查Bagquery字符串的实际内容 - 任何WHERE子句都很重要。



但是,假设你的代码中有 datefrom Dateto 变量,我怀疑你的查询是什么of:

  SELECT  ...  FROM  ...  WHERE  dateColumn  BETWEEN   @ dtfrom   AND   @ dtto  

d,在传递日期之前将日期转换为字符串可能很重要。如果您还在数据库中将日期存储为字符串,则很可能是问题的原因 - 字符串比较不适用于与日期相关的数据。检查您的数据库,看看列声明为什么数据类型。


Heloo guys!
may i know whats wrong with the following code. i am not getting the data in the data set if i am using the following code.

thanks

string Bagquery;

try
{
    string dtfrom = this.datefrom.Value.ToShortDateString();
    string dateto = this.Dateto.Value.ToShortDateString();
    SqlConnection con;
    SqlDataAdapter da;
    DataSet ds;
    DataConnection data = new DataConnection();
    con = data.GetConnection();
    clsBagData bagdata = new clsBagData();
    Bagquery = bagdata.Bagdata(dtfrom, dateto, txtFname.Text, txtSName.Text, cmbEmailID.Text, CmbSubmode.Text);
    con.Open();
    ds = new DataSet();
    da = new SqlDataAdapter(Bagquery, con);
    da.SelectCommand.Parameters.Add("@dtfrom", SqlDbType.VarChar, 11);
    da.SelectCommand.Parameters["@dtfrom"].Value = dtfrom;
    da.SelectCommand.Parameters.Add("@dtto", SqlDbType.VarChar, 11);
    da.SelectCommand.Parameters["@dtto"].Value = dateto;
    da.SelectCommand.Parameters.Add("@RFname", SqlDbType.VarChar, 11);
    da.SelectCommand.Parameters["@RFname"].Value = txtFname.Text;
    da.SelectCommand.Parameters.Add("@RSurname", SqlDbType.VarChar, 11);
    da.SelectCommand.Parameters["@RSurname"].Value = txtSName.Text;
    da.SelectCommand.Parameters.Add("@cmbemailid", SqlDbType.VarChar, 11);
    da.SelectCommand.Parameters["@cmbemailid"].Value = cmbEmailID.Text;
    da.SelectCommand.Parameters.Add("@cmbmode", SqlDbType.VarChar, 11);
    da.SelectCommand.Parameters["@cmbmode"].Value = CmbSubmode.Text;
    da.Fill(ds);
    dataGridView2.DataSource = ds.Tables[0];
    con.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

解决方案

We can''t give you an absolute answer from that code: it misses out far, far too much. For example, your query itself is assembled using a method "bagdata.Bagdata" which you don''t show, and since the query string is probably going to be fairly fundamental to why you don''t get the data you want, you need to look at that.

So the first thing to do is to use the debugger to breakpoint the line:

con.Open();

And examine the actual content of the Bagquery string - any WHERE clause will be significant.

However, give that you have datefrom and Dateto variables in your code, I suspect that your query is something along the lines of:

SELECT ... FROM ... WHERE dateColumn BETWEEN @dtfrom AND @dtto

And that it is probably significant that you convert dates to strings before passing them through. If you are also storing dates as strings in your DB, that is very likely to be the cause of the problem - string comparisons do not work well with date related data. Check your DB and see what datatype the columns are declared as.


这篇关于没有获取ds中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆