对象不包含数据库的定义,也不包含扩展方法数据库

时间:2014-12-24 05:59:22

标签: c# asp.net crystal-reports-2010

我必须在C#中为Crystal Report Viewer声明一个函数,但它显示了一些错误,如; (对象不包含数据库的定义,除了'object'类型的第一个参数之外,没有扩展方法'Database'可以找到(你是否缺少using指令或汇编引用?))我的代码如下所示

 public static object ConnectionForReports(object objReport)
{
        SqlConnection cn = null;
        cn = new SqlConnection(Properties.Settings.Default.CMScon);
        CrystalDecisions.Shared.TableLogOnInfo logOnInfo = null;
        **logOnInfo = objReport.Database.Tables[0].LogOnInfo;**
        logOnInfo.ConnectionInfo.ServerName = cn.DataSource;
        logOnInfo.ConnectionInfo.DatabaseName = cn.Database;
        logOnInfo.ConnectionInfo.UserID = "sa";
        logOnInfo.ConnectionInfo.Password = "SQLadmin";
        **objReport.Database.Tables[0].ApplyLogOnInfo(logOnInfo);**
        return objReport;
}

错误显示在突出显示的行中。

3 个答案:

答案 0 :(得分:1)

问题是

public static object ConnectionForReports(object objReport)

objReport的类型为object

类型object没有数据库概念

总之,你错过了objReport

的合适类型

您需要为此参数声明适当的类型

即。

public static object ConnectionForReports(SomeMagicalType objReport)

或将其转换为适当的类型

((SomeMagicalType)objReport).Database.Tables[0].LogOnInfo

<强>更新

我从来没有使用水晶报告所以这是一个黑暗的镜头

但是,如果您的SomeMagicalType应为ReportDocument

,则表示身份

即你可以改变这个

public static object ConnectionForReports(ReportDocument objReport)

答案 1 :(得分:0)

我在班上宣布了一个简单的方法

 public static object ConnForReport(ReportDocument objReport)
{
SqlConnection cn = null;
cn = new SqlConnection(Properties.Settings.Default.CMScon);
CrystalDecisions.Shared.TableLogOnInfo logOnInfo = null;
logOnInfo = objReport.Database.Tables[0].LogOnInfo;
logOnInfo.ConnectionInfo.ServerName = cn.DataSource;
logOnInfo.ConnectionInfo.DatabaseName = cn.Database;
logOnInfo.ConnectionInfo.UserID = "sa";
logOnInfo.ConnectionInfo.Password = "SQLadmin";
objReport.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
return objReport;
}

然后我在我的按钮点击事件上使用此方法,如下所示:

protected void CmdPreview_Click(object sender, ImageClickEventArgs e)
{
InquiryReports objReport = new InquiryReports();
string vSelectionFormula = null;
ReportDocument objReports = (ReportDocument)MethodClass.ConnForReport(objReport);
vSelectionFormula = "{CompalintView.ComplaintStatus}='" + CboCompStatus.Text + "'";
InquiryRptViewer.SelectionFormula = vSelectionFormula;
InquiryRptViewer.ReportSource = objReports;
Session.Add("CR_Session",objReports);
}

最后,不要忘记在page_load事件中编写这些代码:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session.Remove("CR_Session");
CboComplaintStatusFill();
}
else
{
InquiryRptViewer.ReportSource = Session["CR_Session"];
}

答案 2 :(得分:-1)

无法在db表上找到ApplyLogInfo方法,因为它不存在。

我认为您想要实现的目标如下 -

 private void LogonToReport(string server, string database,
         string ID, string password)
      {
         TableLogOnInfo logonInfo = new TableLogOnInfo();
         foreach(Table table in Report.Database.Tables)
         {
            logonInfo = table.LogOnInfo;
            logonInfo.ConnectionInfo.ServerName = server;
            logonInfo.ConnectionInfo.DatabaseName = database;
            logonInfo.ConnectionInfo.UserID = ID;
            logonInfo.ConnectionInfo.Password = password;

            table.ApplyLogOnInfo(logonInfo);
         }
      }