沒有出現CrystalReportViewer時,須安裝CRforVS_13_0.
新增1個數據集,新增1個數據表,添加二列,列名要和資料庫名一樣.
修改目標Framework
修改app.config,
<startup >改成<startup useLegacyV2RuntimeActivationPolicy ="true">
CrystalReport1.rpt增加數據庫專家
在表單上放入一個CrystalReportViewer1和一個按鈕
程式如下:
Imports CrystalDecisions.Windows.Forms
Imports CrystalDecisions.CrystalReports.EngineImports CrystalDecisions.SharedImports System.Data.SqlClient 'ADO連線Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cn As New SqlConnection("Password=密碼;Persist Security Info=True;User ID=帳號;Initial Catalog=資料庫;Data Source=連線位置") Dim cmd As New SqlCommand("SELECT BADGENUMBER, NAME FROM USERINFO", cn)'sql語言 Dim da As New SqlDataAdapter Dim ds As New DataSetda.SelectCommand = cmd
da.Fill(ds, "DataTable1")'新增的數據集DataSet1.xsd,的數據表DataTable1Dim doc As New ReportDocument
doc.Load("C:\VB\CrystalReport1.rpt")'CrystalReport1.rpt所在位置不能是中文 doc.SetDataSource(ds) CrystalReportViewer1.ReportSource = doc End SubEnd Class
運行畫面如下