|
 |
|
ค้นหา โดยใช้คำสั่ง sql Between @rpStart And @rpTo มันใช้ได้กรณีที่อยู่ระหว่างกลาง |
|
 |
|
|
 |
 |
|
ค้นหา โดยใช้คำสั่ง sql
Between @rpStart And @rpTo มันใช้ได้กรณีที่อยู่ระหว่างกลาง
เช่น ค้นหาวันที่ 4/7/2539 To 10/7/2539
มันจะแสดงดังนี่้ เห็นวันที่ 5/7/2539

แต่พอค้นหาวันที่ 5/7/2539 To 10/7/2539 ดังรูปข้างต้นนี้
มันกลับไม่แสดงวันที่ 5/7/2539

ผมใช้ C#.NET
ฐานข้อมูล MSSQL 2019
รายงานใช้ RDLC REPORT
CODE C# มีดังนี้
Code (C#)
SqlConnection Conn = new SqlConnection(@"Data Source=.;Initial Catalog=Northwind;Integrated Security=True");
private void btnShow_Click(object sender, EventArgs e)
{
string sql = "Select * from orders Where OrderDate Between @rpStart AND @rpTo";
SqlCommand cmd = new SqlCommand(sql, Conn);
cmd.Parameters.AddWithValue("@rpStart", Convert.ToDateTime(dtpStart.Value));
cmd.Parameters.AddWithValue("@rpTo", Convert.ToDateTime(dtpTo.Value));
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
reportViewer1.LocalReport.DataSources.Clear();
ReportDataSource source = new ReportDataSource("DataSet1", dt);
reportViewer1.LocalReport.ReportPath = @"..\..\Report1.rdlc";
reportViewer1.LocalReport.DataSources.Add(source);
ReportParameterCollection rp = new ReportParameterCollection();
rp.Add(new ReportParameter("rpStart", dtpStart.Value.ToString("dd-MM-yyyy 00:00:00")));
rp.Add(new ReportParameter("rpTo", dtpTo.Value.ToString("dd-MM-yyyy 00:00:00")));
reportViewer1.LocalReport.SetParameters(rp);
reportViewer1.RefreshReport();
}
Tag : .NET, Ms SQL Server 2016, Report Others, C#, VS 2017 (.NET 4.x), Windows

|
ประวัติการแก้ไข 2021-07-02 21:04:21
|
 |
 |
 |
 |
Date :
2021-07-02 12:28:47 |
By :
wavezaa |
View :
727 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมทำได้แล้วครับ
คือว่าเปลี่ยน
cmd.Parameters.AddWithValue("@rpStart", Convert.ToDateTime(dtpStart.Value));
cmd.Parameters.AddWithValue("@rpTo", Convert.ToDateTime(dtpTo.Value));
เปลี่ยนเป็น
cmd.Parameters.AddWithValue("@rpStart", Convert.ToDateTime(dtpStart.Value.ToString("dd-MM-yyyy 00:00:00")));
cmd.Parameters.AddWithValue("@rpTo", Convert.ToDateTime(dtpTo.Value.ToString("dd-MM-yyyy 00:00:00")));
|
 |
 |
 |
 |
Date :
2021-07-02 13:11:48 |
By :
wavezaa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่แน่ใจว่า ในฐานข้อมูลเก็บวันที่เป็นแบบไหนครับ แบบเก็บเวลาด้วยหรือเปล่าครับ
และก็รูปแบบวันที่ด้วยเป็น ค.ศ. หรือ พ.ศ. เพราะถ้าส่งไปแบบไม่แปลงอะไรเลยก็จะได้วันที่
ไม่ตรงกับที่ต้องการเอานะครับ หรือไม่ก็ลองเอา Query ไปรันดูก่อนว่าเรากำหนด parameter ถูกไหม
และก็แอบงงอีกว่าทำไหม report ต้องมี parameter อีก ทั้งๆ ที่เราก็ค้นหาข้อมูลที่ต้องการมาแล้ว
|
 |
 |
 |
 |
Date :
2021-07-02 13:18:25 |
By :
บัญดิษฐ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
format ต้องเหมือนกันหรือเปล่า
Code (C#)
// CONVERT(VARCHAR(10),OrderDate,120) จะเป็นรูปแบบ yyyy-MM-dd
string sql = "Select * from orders Where CONVERT(VARCHAR(10),OrderDate,120) Between @rpStart AND @rpTo";
//เห็นใน database เป็น คศ จึงใช้ "en-US" แต่ถ้าเป็น พศ ต้องใช้ "th-TH"
cmd.Parameters.AddWithValue("@rpStart", dtpStart.Value.ToString("yyyy-MM-dd", new CultureInfo("en-US")));
|
 |
 |
 |
 |
Date :
2021-07-03 09:14:36 |
By :
nPointXer |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|