Hi,
Please refer
Now here I have used a Strongly Typed DataSet.
You need to keep same DataSet and fill it from any database of your choice.
It will work provided DataSet is not changed.
For example,
Code can be changed as.
Here the Database can be changed using Connection String. But the Schema of DataSet must be same.
Meaning in all 3 Databases, the Table schema is same i.e. Customers
C#
protected void Page_Load(object sender, EventArgs e)
{
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"));
Customers dsCustomers = GetData("select * from customers", "constr_21");
//Customers dsCustomers = GetData("select * from customers", "constr_22");
//Customers dsCustomers = GetData("select * from customers", "constr_23");
crystalReport.SetDataSource(dsCustomers);
CrystalReportViewer1.ReportSource = crystalReport;
}
private Customers GetData(string query, string constr)
{
string conString = ConfigurationManager.ConnectionStrings[constr].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (Customers dsCustomers = new Customers())
{
sda.Fill(dsCustomers, "DataTable1");
return dsCustomers;
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim crystalReport As New ReportDocument()
crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"))
Dim dsCustomers As Customers = GetData("select * from customers", "constr_21")
'Dim dsCustomers As Customers = GetData("select * from customers", "constr_22")
'Dim dsCustomers As Customers = GetData("select * from customers", "constr_23")
crystalReport.SetDataSource(dsCustomers)
CrystalReportViewer1.ReportSource = crystalReport
End Sub
Private Function GetData(query As String, constr As String) As Customers
Dim conString As String = ConfigurationManager.ConnectionStrings(constr).ConnectionString
Dim cmd As New SqlCommand(query)
Using con As New SqlConnection(conString)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dsCustomers As New Customers()
sda.Fill(dsCustomers, "DataTable1")
Return dsCustomers
End Using
End Using
End Using
End Function