男人.No boy no cry

彪悍的人生,不需要解釋...

导航

如何在ASP中用水晶报表访问SQL Server数据库?

Posted on 2006-07-24 08:26  Piccolo Goo  阅读(935)  评论(0)    收藏  举报
请问如何在ASP中用水晶报表访问SQL Server数据库?
            水晶报表中带的例子是访问Access数据库的,如果要访问SQL Server
            数据库,该怎样写?我写的总是出错.因为在Access中不需要用户名和密码的验证
            但在SQL中需要,所以加上验证后的语句总是出错.还有如何把报表显示出来.
            谢谢!
            
回复人: yonghengdizhen(等季节一过,繁花就凋落) ( ) 信誉:127 2002-12-03 17:22:47Z 得分:0
?
<!-- #include file="public_functions.asp" -->
                        <%
                        Dim sConfigFilePath,reportname,ReportFolder
                        sConfigFilePath = Request.ServerVariables("PATH_TRANSLATED")
                        While (Right(sConfigFilePath, 1) <> "\" And Len(sConfigFilePath) <> 0)
                        iLen = Len(sConfigFilePath) - 1
                        sConfigFilePath = Left(sConfigFilePath, iLen)
                        Wend
                        sConfigFilePath=sConfigFilePath & "Config.xml"
                        charset=ReadConfigureFile(sConfigFilePath,"charset")
                        %>
                        <meta http-equiv="Content-Type" content="text/html; charset=<%=charset%>">
                        <title>Crystal Reports</title>
                        <%
                        for each sItemName in Request.Form
                        if sItemName<>"" then
                        sItemValue=Request.Form(sItemName)
                        sTmpEval="dim " & sItemName
                        execute(sTmpEval)
                        sTmpEval=sItemName&"="""&replace(sItemValue,"""","""""")&""""
                        execute(sTmpEval)
                        end if
                        next
                        ReportFolder=ReadConfigureFile(sConfigFilePath,"sFolder")
                        if ReportName="" then
                        Response.Write "Invalid Report Name"
                        Response.End
                        end if
                        if Instr(1,ReportName,".rpt")>0 then
                        reportname=ReportName
                        else
                        reportname=ReportName&".rpt"
                        end if
                        reportname=ReportFolder&"\"&reportname
                        Dim DBInfosArr(3,2)
                        'For OOCL LSD Database.
                        DBInfosArr(0,0)=ReadConfigureFile(sConfigFilePath,"sConn_OOCL_LSD")'DSN Name
                        DBInfosArr(1,0)=ReadConfigureFile(sConfigFilePath,"sDBName_OOCL_LSD")'Database Name
                        DBInfosArr(2,0)=ReadConfigureFile(sConfigFilePath,"sOOCL_LSD_UserID") 'User id
                        DBInfosArr(3,0)=ReadConfigureFile(sConfigFilePath,"sOOCL_LSD_Pwd")'password.
                        'For OOCL Master Database
                        DBInfosArr(0,1)=ReadConfigureFile(sConfigFilePath,"sConn_OOCL_Master")'DSN Name
                        DBInfosArr(1,1)=ReadConfigureFile(sConfigFilePath,"sDBName_OOCL_Master")'Database Name
                        DBInfosArr(2,1)=ReadConfigureFile(sConfigFilePath,"sOOCL_Master_UserID") 'User id
                        DBInfosArr(3,1)=ReadConfigureFile(sConfigFilePath,"sOOCL_Master_Pwd")'password.
                        'For OOCL Report Database.
                        DBInfosArr(0,2)=ReadConfigureFile(sConfigFilePath,"sConn_OOCL_Report")'DSN Name
                        DBInfosArr(1,2)=ReadConfigureFile(sConfigFilePath,"sDBName_OOCL_Report")'Database Name
                        DBInfosArr(2,2)=ReadConfigureFile(sConfigFilePath,"sOOCL_Report_UserID") 'User id
                        DBInfosArr(3,2)=ReadConfigureFile(sConfigFilePath,"sOOCL_Report_Pwd")'password.
                        %>
                        <!-- #include file="AlwaysRequiredSteps.asp" -->
                        <%
                        ' WORKING WITH SETLOGONINFO
                        '
                        ' The datasource here is called "Automation".  It is a System
                        ' Datasource, and points to the "pubs" database, which is installed
                        ' with SQL Server. You will also need to change your user id and
                        ' password.
                        'Create a reference to the tables collection of the main report
                        Set mainReportTableCollection = Session("oRpt").Database.Tables
                        For Each Table in mainReportTableCollection
                        DatabaseName=Table.LogOnDatabaseName
                        For I =Lbound(DBInfosArr,2) to Ubound(DBInfosArr,2)
                        if instr(1,UCase(DBInfosArr(1,I)),Ucase(DatabaseName),1)>0 then
                        DSNName=DBInfosArr(0,I)
                        DBName=DBInfosArr(1,I)
                        UserID=DBInfosArr(2,I)
                        Pwd=DBInfosArr(3,I)
                        Table.SetLogonInfo cstr(DSNName), cstr(DBName), cstr(UserID), cstr(Pwd)
                        Exit for
                        end if
                        Next
                        Next
                        '************************************
                        'Set the Main Report's Parameters' value
                        set Params = Session("oRpt").Parameterfields
                        For each Param in Params
                        ParamType=Param.valuetype
                        ParamName=GetParamName(Param.Name)
                        ParamValue=Eval(ParamName)
                        Select Case ParamType
                        Case 7'Number
                        Call Param.SetCurrentValue (dfVntToInt(ParamValue), 7)
                        Case 8'Currency
                        Call Param.SetCurrentValue (Cdbl(ParamValue), 8)
                        Case 10'Date
                        Call Param.SetCurrentValue (CDate(ParamValue), 10)
                        Case 12'String
                        Call Param.SetCurrentValue (CStr(ParamValue), 12)
                        End Select
                        Next
                        set Params=nothing
                        '************************************
                        Set CRXSections = Session("oRpt").Sections
                        II=1
                        For Each CRXSection In CRXSections
                        Response.Write II &"<BR>"
                        II=II+1
                        'In each section, you get all the objects in the section.
                        Set CRXReportObjects = CRXSection.ReportObjects
                        'You cycle through the objects.
                        For Each CRXReportObject In CRXReportObjects
                        '1---Text Object
                        '2---Fields Object
                        '3---Line
                        '4---Box
                        'Sub Report---5
                        If CRXReportObject.Kind = 5 Then
                        Set CRXSubreportObj = CRXReportObject
                        Set CRXSubreport = CRXSubreportObj.OpenSubreport
                        '************************************
                        'Set The Sub Reports Parameters'Value
                        Set Params=CRXSubreport.Parameterfields
                        For each Param in Params
                        ParamType=Param.valuetype
                        ParamName=GetParamName(Param.name)
                        ParamValue=Eval(ParamName)
                        if Instr(1,ParamName,"?Pm-")=0 then
                        Select Case ParamType
                        Case 7'Number
                        Call Param.SetCurrentValue (dfVntToInt(ParamValue), 7)
                        Case 8'Currency
                        Call Param.SetCurrentValue (CDbl(ParamValue), 8)
                        Case 10'Date
                        Call Param.SetCurrentValue (CDate(ParamValue), 10)
                        Case 12'String
                        Call Param.SetCurrentValue (CStr(ParamValue), 12)
                        End Select
                        end if
                        Next
                        set Params=nothing
                        '************************************
                        Set subReportOneTablesCollection = CRXSubreport.Database.Tables
                        For Each Table in subReportOneTablesCollection
                        DatabaseName=Table.LogOnDatabaseName
                        Response.Write "Sub Report----" & DatabaseName & "<BR>"
                        For I =Lbound(DBInfosArr,2) to Ubound(DBInfosArr,2)
                        if instr(1,UCase(DBInfosArr(1,I)),Ucase(DatabaseName),1)>0 then
                        DSNName=DBInfosArr(0,I)
                        DBName=DBInfosArr(1,I)
                        UserID=DBInfosArr(2,I)
                        Pwd=DBInfosArr(3,I)
                        Table.SetLogonInfo cstr(DSNName), cstr(DBName), cstr(UserID), cstr(Pwd)
                        Exit for
                        end if
                        Next
                        Next
                        End If
                        Next
                        Next
                        %>
                        <!-- #include file="MoreRequiredSteps.asp" -->
                        <!-- #include file="SmartVieweractivex.asp" -->
                        至于这些包含文件在crystal report的安装盘上都有的.
                        客户段则使用一个active x web控件同步显示数据
                        
Top
回复人: yonghengdizhen(等季节一过,繁花就凋落) ( ) 信誉:127 2002-12-03 17:23:16Z 得分:0
?
<%
                        'This file contains the HTML code to instantiate the Smart Viewer ActiveX.
                        '
                        'You will notice that the Report Name parameter references the rptserver.asp file.
                        'This is because the report pages are actually created by rptserver.asp.
                        'Rptserver.asp accesses session("oApp"), session("oRpt") and session("oPageEngine")
                        'to create the report pages that will be rendered by the ActiveX Smart Viewer.
                        '
                        %>
                        <HTML>
                        <HEAD>
                        <TITLE>Seagate ActiveX Viewer</TITLE>
                        </HEAD>
                        <BODY BGCOLOR=C6C6C6 ONUNLOAD="CallDestroy();">
                        <OBJECT ID="CRViewer"
                        CLASSID="CLSID:C4847596-972C-11D0-9567-00A0C9273C2A"
                        WIDTH=100% HEIGHT=95%
                        CODEBASE="/viewer/activeXViewer/activexviewer.cab#Version=8,0,0,224">
                        <PARAM NAME="EnableRefreshButton" VALUE=1>
                        <PARAM NAME="EnableGroupTree" VALUE=1>
                        <PARAM NAME="DisplayGroupTree" VALUE=1>
                        <PARAM NAME="EnablePrintButton" VALUE=1>
                        <PARAM NAME="EnableExportButton" VALUE=1>
                        <PARAM NAME="EnableDrillDown" VALUE=1>
                        <PARAM NAME="EnableSearchControl" VALUE=1>
                        <PARAM NAME="EnableAnimationControl" VALUE=1>
                        <PARAM NAME="EnableZoomControl" VALUE=1>
                        </OBJECT>
                        <SCRIPT LANGUAGE="VBScript">
                        <!--
                        Sub Window_Onload
                        On Error Resume Next
                        Dim webBroker
                        Set webBroker = CreateObject("WebReportBroker.WebReportBroker")
                        Dim webSource
                        Set webSource = CreateObject("WebReportSource.WebReportSource")
                        webSource.ReportSource = webBroker
                        webSource.URL = "rptserver.asp"
                        webSource.PromptOnRefresh = True
                        CRViewer.ReportSource = webSource
                        CRViewer.ViewReport
                        End Sub
                        -->
                        </SCRIPT>
                        <script language="javascript">
                        function CallDestroy()
                        {
                        window.open("Cleanup.asp.html");
                        }
                        </script>
                        </BODY>
                        </HTML>
                        
Top
回复人: yonghengdizhen(等季节一过,繁花就凋落) ( ) 信誉:127 2002-12-03 17:25:26Z 得分:0
?
第一个回贴中设置login id的步骤很重要..
                        否则每个子报表都必须手动输入login信息
                        
Top
回复人: dy518(大有) ( ) 信誉:98 2002-12-04 09:54:57Z 得分:0
?
请问:
                        以下几个文件是什么文件,哪里可以找到?
                        public_functions.asp
                        AlwaysRequiredSteps.asp
                        MoreRequiredSteps.asp
                        谢谢!
                        
Top

该问题已经结贴