水晶报告日期 [英] crystal report date to date

查看:70
本文介绍了水晶报告日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我遇到了水晶报告的正式问题.我从以下网站获取了代码:
http://csharp.net-informations.com/crystal- reports/csharp-crystal-reports-date-to-date.htm [

Hello,
I''ve crystal report formal problem .I took the code from this website:
http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-date-to-date.htm[^]
,but when I do the formal it gave error string required.

[edit]Additional information copied from comment below:[/edit]
Also, I found this code and did some changes on it ,but the problem is it gives me all the date?

private void button1_Click(object sender, EventArgs e)
        {
            string from_date = textBox1.Text ;
            string to_date = textBox2.Text;
            SqlConnection con=new SqlConnection (@"Data Source=.\Ahmed;Initial Catalog=Market;Integrated Security=True");
            String data = "SELECT * from Product  where dat>='"+from_date+"' and dat<='"+to_date+"'";
            con.Open();
            SqlDataAdapter ad1 = new SqlDataAdapter(data, con);
            DataSet ds1 = new DataSet();
            ad1.Fill(ds1);
            ReportDocument cr  = new ReportDocument();
            cr.Load(@"C:\Users\Ahmed\Documents\Visual Studio 2010\Projects\WindowsFormsApplication9\WindowsFormsApplication9\CrystalReport4.rpt");
 
            cr.SetDataSource(ds1.Tables["product"]);
            crystalReportViewer1.ReportSource = cr;
 
        }

推荐答案

问题是将字符串值转换为日期.为此,您应该使用实际的SqlCommand和SqlParameters(以防止SQL注入).您还需要将用户在文本框中输入的值转换为DateTime对象.您还应该在WHERE子句中使用Between,而不是<>.

您的代码应类似于以下内容:
***注意:我只是在浏览器中输入了它,所以它可能无法编译***
The problem is converting the string values to dates. To accomplish this you should use an actual SqlCommand and SqlParameters (to prevent SQL injection). You are also going to need to convert the values the user entered in a the text box to DateTime objects. You should also use Between in your WHERE clause instead of <>.

Your code should look similar to this:
*** Note: I just typed this into the browser so it probably wont compile ***
private void button1_Click(object sender, EventArgs e)
        {
            string from_date = textBox1.Text ;
            string to_date = textBox2.Text;
            DataSet ds1 = new DataSet();
            ReportDocument cr  = new ReportDocument();

            using (SqlConnection con=new SqlConnection (@"Data Source=.\Ahmed;Initial Catalog=Market;Integrated Security=True")){
                String data = "SELECT * from Product  where dat BETWEEN @StartDate AND @EndDate";
                con.Open();
                using (SqlCommand cmd = new SqlCommand(data, conn))
                {
                    // you are going to need to parse these dates in their expected format
                    // Convert.ToDateTime has some overrides for that
                    // See below for some links for some reading
                    cmd.Parameters.Add(new SqlParameter("@StartDate", Convert.ToDateTime(from_date));
                   cmd.Parameters.Add(new SqlParameter("@EndDate", Convert.ToDateTime(to_date));
                   using (SqlDataAdapter ad1 = new SqlDataAdapter(cmd))
                   {
                      ad1.Fill(ds1);
                   }
                }
            }

            cr.Load(@"C:\Users\Ahmed\Documents\Visual Studio 2010\Projects\WindowsFormsApplication9\WindowsFormsApplication9\CrystalReport4.rpt");
            
            cr.SetDataSource(ds1.Tables["product"]);
            crystalReportViewer1.ReportSource = cr;
 
        }



您可能应该阅读这些内容,所以这里有一些链接:
运算符之间的SQL [ SqlCommand类 [ SqlParameter类 [使用语句的C# [ DateTime.TryParseExact [ Convert.ToDateTime [



You should probably read up on this stuff so here are some links:
SQL Between Operator[^]
SqlCommand Class[^]
SqlParameter Class[^]
C# using Statement[^]
DateTime.TryParseExact[^]
Convert.ToDateTime[^]


这篇关于水晶报告日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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