转换Crystalreport的提供程序来自' SQLNCLI'到' SQLOLEDB'按代码(c#)

时间:2015-08-12 04:12:50

标签: c# .net crystal-reports

是否可以从

我尝试使用以下代码,但无效。

TableLogOnInfo logoninfo;
            foreach (CrystalDecisions.CrystalReports.Engine.Table tbcurrent in rptH.Database.Tables)
            {
                logoninfo = new TableLogOnInfo();
                logoninfo.ConnectionInfo.DatabaseName = connectionInfo.DatabaseName;
                logoninfo.ConnectionInfo.ServerName = connectionInfo.ServerName;
                logoninfo.ConnectionInfo.UserID = connectionInfo.UserID;
                logoninfo.ConnectionInfo.Password = connectionInfo.Password;
                tbcurrent.ApplyLogOnInfo(logoninfo);
            }
            rptH.SetDatabaseLogon(connectionInfo.UserID, connectionInfo.Password, connectionInfo.ServerName, connectionInfo.DatabaseName, true);
            for (int i = 0; i < rptH.Subreports.Count; i++)
            {
                foreach (CrystalDecisions.CrystalReports.Engine.Table tbcurrent in rptH.Subreports[i].Database.Tables)
                {
                    logoninfo = new TableLogOnInfo();
                    logoninfo.ConnectionInfo.DatabaseName = connectionInfo.DatabaseName;
                    logoninfo.ConnectionInfo.ServerName = connectionInfo.ServerName;
                    logoninfo.ConnectionInfo.UserID = connectionInfo.UserID;
                    logoninfo.ConnectionInfo.Password = connectionInfo.Password;
                    tbcurrent.ApplyLogOnInfo(logoninfo);
                }
                rptH.Subreports[i].SetDatabaseLogon(connectionInfo.UserID, connectionInfo.Password, connectionInfo.ServerName, connectionInfo.DatabaseName, true);
            }

1 个答案:

答案 0 :(得分:1)

我不确定是否可以这样做,因为连接类型似乎存储在.rpt文件中。 但您可以尝试使用原生.Net API将数据库选择检索到System.Data.DataSet / System.Data.DataTable实例,并调用Table.SetDataSource()设置为表数据源,因此您无需关心TableLogOnInfo对象,但请确保该方案与报告的方案相匹配。

以下示例代码来自Crystal Reports .NET API指南:

private void SetDataSource
   (string conn, string query, DataSet dataSet)
{
   OleDbConnection oleConn = new OleDbConnection(conn);
   OleDbDataAdapter oleAdapter = new OleDbDataAdapter();
   oleAdapter.SelectCommand = new OleDbCommand(query, oleConn);

   oleAdapter.Fill(dataSet, "Customer");

   reportDocument.Database.Tables["Customer"].SetDataSource (dataSet);
}

此外,ReportDocument也有SetDataSource()方法,因此您可以为整个报告设置数据源。