Create a new Windows Forms Application project in VS2010, add a Crystal Report "Crystalreport1.rpt" to the project, and then click the right mouse button on the item to change the target frame to ". Net Framework 4"
Open the app.config file, a "uselegacyv2runtimeactivationpolicy=" true "property" on the "Startup" node
Copy Code code as follows:
<startup uselegacyv2runtimeactivationpolicy= "true" >
<supportedruntime version= "v4.0" sku= ". netframework,version=v4.0 "/>
</startup>
In the Form1 form, drag a crystal the Viewer control from the toolbox, double-click the Form form, double-click the form form, not crystal the show viewer, and write the following code in the Form_Load event in the background:
Copy Code code as follows:
private void Form1_Load (object sender, EventArgs e)
{
String connstr = "Data source=.\\sqlexpress;initial catalog=dbtest; User Id=sa; Password=test ";
SqlConnection conn = new SqlConnection (CONNSTR);
Conn. Open ();
Try
{
String sql = "SELECT * from Customer where email!= ' test@gmail.com '";
SqlDataAdapter SDA = new SqlDataAdapter (SQL, conn);
DataSet ds = new DataSet ();
Sda. Fill (ds, "tmptable");
String Reportpath = System.Windows.Forms.Application.StartupPath + @ "\crystalreport1.rpt";
Reportdocument rd = new reportdocument ();
Rd. Load (Reportpath);
Rd. Setdatasource (ds. Tables[0]. DefaultView);
This.crystalReportViewer1.ReportSource = rd;
}
catch (Exception ex)
{
throw new Exception (ex. Message.tostring ());
}
Finally
{
Conn. Close ();
}
}
That's OK.