Bài 5: Tạo dynamic reports trong C#

Bài 5: Tạo dynamic reports trong C#

Mấy bài trước, chúng ta tạo report từ những database đã được chọn sẵn, bây giờ ta tạo report từ kết quả của các query SQL lúc runtime.

Ta cũng tạo project winform và cũng thêm vào 1 DataSet như bài trên kia nhưng ko kết nối vào database nào cả. Trên Dataset, ta cũng thêm 1 datatable vào DataSet đó, trên Datatable ta tạo mấy cái column như sau:

Bài 5: Tạo dynamic reports trong C#

Tạo thêm 1 cái crytal report như baì trước!
Các bạn thực hiện như các bước ở các bài trên tớ làm, cũng sử dụng ProjectData và chọn tất cả cac column của cái Datatable vừa tạo nhá.

Bi giờ ta sẽ thiết kế 1 cài Form như bên giưới,report sẽ phụ thuộc vào cac queyry lúc runtime:

Viết 1 hàm xử lý cho các query nhập vào :

public string procesSQL()
        {
            string sql = null;
            string inSql = null;
            string firstPart = null;
            string lastPart = null;
            int selectStart = 0;
            int fromStart = 0;
            string[] fields = null;
            string[] sep = { "," };
            int i = 0;
            TextObject MyText ;

            inSql = textBox1.Text;
            inSql = inSql.ToUpper();

            selectStart = inSql.IndexOf("SELECT");
            fromStart = inSql.IndexOf("FROM");
            selectStart = selectStart + 6;
            firstPart = inSql.Substring(selectStart, (fromStart - selectStart));
            lastPart = inSql.Substring(fromStart, inSql.Length - fromStart);

            fields = firstPart.Split(',');
            firstPart = "";
            for (i = 0; i <= fields.Length - 1; i++)
            {
                if (i > 0)
                {
                    firstPart = firstPart + ", " + fields[i].ToString() + " AS COLUMN" + (i + 1);
                    firstPart.Trim();

                    MyText = (TextObject) objRpt.ReportDefinition.ReportObjects[i+1];
                    MyText.Text = fields[i].ToString();
                }
                else
                {
                    firstPart = firstPart + fields[i].ToString() + " AS COLUMN" + (i + 1);
                    firstPart.Trim();

                    MyText = (TextObject)objRpt.ReportDefinition.ReportObjects[i+1];
                    MyText.Text = fields[i].ToString();
                }
            }
            sql = "SELECT " + firstPart + " " + lastPart;
            return sql;
        }

Và viết code cho sư kiện của button :

private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cnn ;
            string connectionString = null;
            string sql = null;
            connectionString = "data source=SERVER NAME;initial catalog=crystaldb;user id=USER NAME;password=PASSWORD;";
            cnn = new SqlConnection(connectionString);
            cnn.Open();
            sql = procesSQL();
            SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
            DataSet1 ds = new DataSet1();
            dscmd.Fill(ds, "Product");
            objRpt.SetDataSource(ds.Tables[1]);
            crystalReportViewer1.ReportSource = objRpt;
            crystalReportViewer1.Refresh();
        }

À các bạn nhớ using mấy cái namespace cần thiết nhá
Còn 1 điều nữa là mọi vi dụ đều dùng database ở bài đâu,khi chạy ví dụ thì các bạn nhớ chỉnh lại chuỗi kết nối cho phù hợp
 

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ả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

 
Kho phần mềm nguồn mở hosted tại Việt Nam

Kho phần mềm nguồn mở hosted tại Việt Nam

Một số công ty, tổ chức tại Việt nam hiện đã cung cấp các server lưu trữ phần mềm nguồn mở. Tìm kiếm, tải về và cập nhật từ các kho này đương nhiên là nhanh hơn nhiều so với các kho ở nước ngoài.

Hướng dẫn 3 cách prompt for user input in Drush

Hướng dẫn 3 cách prompt for user input in Drush

Drush is awesome. It makes Drupal development much easier. Not only that it comes already packed with a bunch of useful commands, but you can declare your own with great ease.

Nguồn tài nguyên giúp learning Drupal 8

Nguồn tài nguyên giúp learning Drupal 8

So you are interested in Drupal 8, right? I know I am, but with all these fundamental changes, many developers out there (including myself) need to do some serious work adapting their skillset to be able to work with it.

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

 

Diet con trung