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

 
Hội thảo Drupal 8 xoay quanh sprint designed tại Belgium

Hội thảo Drupal 8 xoay quanh sprint designed tại Belgium

During the 5-day sprint, we worked on an impressive 51 critical issues, 28 of which are already fixed.

Window, Window 7, Mac iOS 6, Mac OS, MacBook Air, MacBook Pro

Hướng dẫn thiết lập mạng riêng ảo (VPN) của riêng bạn trên Windows và Mac

Mạng riêng ảo (hay Virtual Private Network) là một mạng thường dành riêng để kết nối các máy tính trong nhà hay trong một tổ chức với nhau thông qua Internet

Đợt giảm giá thứ hai trong tháng của một số smartphone năm 2013

Đợt giảm giá thứ hai trong tháng của một số smartphone năm 2013

Đây là đợt giảm giá thứ hai chỉ trong vòng một tháng nay, tập trung nhiều vào các model tầm trung từ 4-6 triệu đồng.

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

 

Diet con trung