c#水晶报表连接mysql_C# 水晶报表示例

本示例展示如何在C#应用中利用水晶报表显示从MySQL数据库获取的数据。首先建立SQL连接,然后通过SqlCommand查询Customers表中的所有数据。查询结果填充到DataSet中,再设置水晶报表的数据源为DataSet,最后在CrystalReportViewer中展示报表。
摘要由CSDN通过智能技术生成

【实例简介】

【实例截图】

839bd6b115ce6d4bf516462b48801ada.png

【核心代码】

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data.SqlClient;

using System.Data;

namespace Export

{

///

/// Form2 的摘要说明。

///

public class Form2 : System.Windows.Forms.Form

{

private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;

private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;

private System.Data.SqlClient.SqlCommand sqlSelectCommand1;

private System.Data.SqlClient.SqlCommand sqlInsertCommand1;

private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;

private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;

private System.Data.SqlClient.SqlConnection sqlConnection1;

private Export.DataSet2 dataSet21;

private System.Windows.Forms.Button button1;

///

/// 必需的设计器变量。

///

private System.ComponentModel.Container components = null;

public Form2()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();

SqlCommand cmd1 = new SqlCommand();

cmd1.Connection = sqlConnection1;

cmd1.CommandType = CommandType.Text;

cmd1.CommandText ="SELECT * FROM Customers ";

DataSet ds1 = new DataSet();

SqlDataAdapter da1 = new SqlDataAdapter(cmd1);

da1.Fill(ds1,"Customers");

CrystalReport1 ocr = new CrystalReport1();

ocr.SetDataSource(ds1);

this.crystalReportViewer1.ReportSource=ocr;

//

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码

//

}

///

/// 清理所有正在使用的资源。

///

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows 窗体设计器生成的代码

///

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

///

private void InitializeComponent()

{

this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();

this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();

this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();

this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();

this.dataSet21 = new Export.DataSet2();

this.button1 = new System.Windows.Forms.Button();

((System.ComponentModel.ISupportInitialize)(this.dataSet21)).BeginInit();

this.SuspendLayout();

//

// crystalReportViewer1

//

this.crystalReportViewer1.ActiveViewIndex = -1;

this.crystalReportViewer1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)

| System.Windows.Forms.AnchorStyles.Left)

| System.Windows.Forms.AnchorStyles.Right)));

this.crystalReportViewer1.Location = new System.Drawing.Point(0, 8);

this.crystalReportViewer1.Name = "crystalReportViewer1";

this.crystalReportViewer1.ReportSource = null;

this.crystalReportViewer1.Size = new System.Drawing.Size(680, 416);

this.crystalReportViewer1.TabIndex = 0;

//

// sqlDataAdapter1

//

this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;

this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;

this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;

this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMap

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值