在VS中新建一个“Windows 窗体应用程序”项目在该项目中添加一个水晶报表“CrystalReportrpt”然后在项目上点击鼠标右键属性将“目标框架”改为“Net Framework ”
打开appconfig文件在“startup”节点一个“useLegacyVRuntimeActivationPolicy="true"”属性
复制代码 代码如下:
<startup useLegacyV
RuntimeActivationPolicy="true">
<supportedRuntime version="v
" sku="
NETFramework
Version=v
"/>
</startup>
在Form窗体中从工具箱拖出一个Crystal Report Viewer控件双击Form窗体是双击Form窗体不是Crystal Report Viewer在后台的Form_Load事件中写入如下代码
复制代码 代码如下:
private void Form
_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 = SystemWindowsFormsApplicationStartupPath + @"CrystalReportrpt";
ReportDocument rd = new ReportDocument();
rdLoad(reportPath);
rdSetDataSource(dsTables[]DefaultView);
thiscrystalReportViewerReportSource = rd;
}
catch (Exception ex)
{
throw new Exception(exMessageToString());
}
finally
{
connClose();
}
}
这样就OK了