日期:2010-06-21  浏览次数:20460 次

经常有网友问这个问题,长时间不看自己也忘记了,今天又碰到一个网友提问,没辙,花了点下班时间把几个步骤记录如下,请指正!

1、新建一个windows application:CristalTest;

2、新添加一个数据集DataSet1.xsd
3、在DataSet1里添加表DataTable1
在DataTable1里添加四列Column1,Column2,Column3,Column4 ,保存
4、添加一个水晶报表文件CrystalReport1.rpt
设置数据源:项目数据(Project Data)-->Ado.net DataSets-->***.DataSet1-->DataTable1
5、把Column1,Column2,Column3,Column4拖到水晶报表的细节(Details)中
6、添加一个控件CrystalReportViewer1到Form1中

7、然后添加Form1_Load的代码 

        Dim m_sqlConn As SqlConnection
        Dim m_strSqlConn As String
        m_strSqlConn = "Data Source=localhost; initial catalog=pubs;User id=sa;password="
        m_sqlConn = New SqlConnection(m_strSqlConn)
        Try
            Dim sqlAdp As SqlDataAdapter
            Dim strSql As String
            Dim ds = New DataSet()
            m_sqlConn.Open()
            strSql = "SELECT au_lname AS Column1, au_fname AS Column2, phone AS Column3, address AS Column4 FROM dbo.authors"
            sqlAdp = New SqlDataAdapter(strSql, m_sqlConn)
            sqlAdp.Fill(ds)

            Dim crReportDocument = New CrystalReport1()
            crReportDocument.SetDataSource(ds.Tables(0))
            CrystalReportViewer1.ReportSource = crReportDocument
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try


8、在form1.vb文件头加

Imports System.Data
Imports System.Data.SqlClient 
9、 m_strSqlConn = "Data Source=localhost; initial catalog=pubs;User id=sa;password="这个可能要修改成你的机器的Sql Server连接信息