Setting datasource programmatically

Stimulsoft Reports.WPF discussion
Post Reply
bakersman
Posts: 3
Joined: Tue Aug 17, 2010 3:39 am
Location: Home

Setting datasource programmatically

Post by bakersman »

Hi there.

Could someone show me in C# how to set a reports datasource to a mssql server and database programmatically, please?

Regards
Renier
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Setting datasource programmatically

Post by Alex K. »

Hello,

1. Use DataSet

Code: Select all

using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand myCommand = new SqlCommand("SELECT * FROM PRODUCTS", connection);
                SqlDataAdapter dataAdapter = new SqlDataAdapter(myCommand);
                DataSet dataSet = new DataSet("DataBase");
                dataAdapter.Fill(dataSet);
                StiReport stiReport = new StiReport();
                stiReport.RegData(dataSet);
                stiReport.Design();
            }
2. Use SqlSorce
Design:

Code: Select all

   Stimulsoft.Report.Dictionary.StiSqlDatabase db = new Stimulsoft.Report.Dictionary.StiSqlDatabase("test", "");
            db.ConnectionString = connectionString;
            Stimulsoft.Report.Dictionary.StiSqlSource userSource = new Stimulsoft.Report.Dictionary.StiSqlSource();
            userSource.NameInSource = "test";
            userSource.Alias = "PRODUCTS";
            userSource.Name = "PRODUCTS";
            userSource.SqlCommand = "SELECT * FROM PRODUCTS";
            StiReport stiReport = new StiReport();
            stiReport.Dictionary.Databases.Add(db);
            stiReport.Dictionary.DataSources.Add(userSource);
            stiReport.Dictionary.Synchronize();
            stiReport.Design();

        Show:
            StiReport stiReport = new StiReport();
            stiReport.Load(@"D:\Report.mrt");
            stiReport.Show();
Thank you.
Post Reply