1.建立項目Print
2.主介面添加一個按鈕,和水晶報表預覽處理常式
3.右擊資源管理員,添加一個資料集
3.按步驟添加自己的資料庫到資料集中
4.添加完畢,再右擊項目,建立添加水晶報表
5.接著選擇報表裡面需要的表,欄位,按步驟操作,完成後如下
6.接著雙擊剛才添加的按鈕,裡面主要代碼如下
步驟是串連資料庫,建立與資料庫連接的適配器SqlDataAdapter,把剛才添加的資料集MyReortData,建立一個,然後Fill添加到SqlDataAdapter;接著建立水晶報表,設定資料來源,接著再給水晶報表預覽處理常式設定資料來源
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace TestLogin{ public partial class Form_print : Form { public Form_print() { InitializeComponent(); } SqlDataAdapter da; private void button1_Click(object sender, EventArgs e) { DataOpt daa = new DataOpt(); daa.SQLConn(); MyReortData mydata = new MyReortData(); da = new SqlDataAdapter("SELECT * FROM score INNER JOIN student ON score.studentno = student.studentno", daa.conn); da.Fill(mydata, "DataTable1"); CrystalReport1 cr = new CrystalReport1(); cr.SetDataSource(mydata); crystalReportViewer1.ReportSource = cr; } }}