Page 1 of 1

Setting datasource programmatically

Posted: Tue Aug 17, 2010 3:42 am
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

Setting datasource programmatically

Posted: Tue Aug 17, 2010 4:22 am
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.