Bài 4 : Tạo reports từ Sql query

Bài 4 : Tạo reports từ Sql query

Ở bài này tui sẽ tạo report từ kết quả của các query trong Sql, vẫn sử dụng DataSet. Tui sẽ tạo report cho bảng Product trong cái database đã nói ở bài trên cùng nhé,câu truy vấn của chúng ta như sau :

sql = "SELECT Product_id,Product_name,Product_price FROM Product";

Tạo mới 1 project winform và add vào 1 DataSet mới,trên DataSet đó tạo thêm 1 DataTable, và trên DataTable đó ta add 3 column vào nhá

Bài 4 : Tạo reports từ Sql query

Tuơng tự như bài vừa trên, tui sẽ tạo thêm 1 crytal report và vẫn sử dụng ProjectData :

Ở bước kế tiếp tui sẽ chọn cả 3 fields Product_id,Product_name,Product_price.

Đã xong phần thiết kế report. Theo truyền thống, ta lai tạo 1 button và 1 crytalReportViewer như mấy cái trên kia nhé

private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cnn;
            string connectionString = null;
            string sql = null;

            connectionString = "server=.\\sqlexpress;database=example;integrated security=true";
            cnn = new SqlConnection(connectionString);
            cnn.Open();
            sql = "SELECT Product_id,Product_name,Product_price FROM Product";
            SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
            DataSet1 ds = new DataSet1();
            dscmd.Fill(ds, "Product");
            MessageBox.Show(ds.Tables[1].Rows.Count.ToString());
            cnn.Close();

            CrystalReport1 objRpt = new CrystalReport1();
            objRpt.SetDataSource(ds.Tables[1]);
            crystalReportViewer1.ReportSource = objRpt;
            crystalReportViewer1.Refresh();

        }
Bạn thấy bài viết này như thế nào?: 
Average: 9.5 (2 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
How to Check Your Facebook Account is Hacked or Not

How to Check Your Facebook Account is Hacked or Not

Has Your Facebook Account Been Hacked? Today here we will let you know How to Check Your Facebook Account is Hacked or Not.

Tính năng mới trong HĐH Mountain Lion phiên bản Beta

Tính năng mới trong HĐH Mountain Lion phiên bản Beta

Nằm trong Notification Center, tính năng "Đừng làm phiền" sẽ là bức tường cản phá những điều gây chi phối trong giờ làm việc trên Mac.

Lợi và hại của việc sử dụng Facebook đối với học sinh

Lợi và hại của việc sử dụng Facebook đối với học sinh

Ngày 4/2/2004, một trang mạng xã hội có tên “Thefacebook” ra đời. Tính đến nay đã tròn 10 năm.

Công ty diệt chuột T&C

 

Diet con trung