在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了
From:http://tw.wingwit.com/Article/program/net/201311/14223.html