可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm using Crystal report to generate reports to my application, this is the code i used:
private void button5_Click(object sender, EventArgs e) { ReportDocument cryRpt = new ReportDocument(); TableLogOnInfos crtableLogoninfos = new TableLogOnInfos(); TableLogOnInfo crtableLogoninfo = new TableLogOnInfo(); ConnectionInfo crConnectionInfo = new ConnectionInfo(); Tables CrTables; cryRpt.Load("C:\\Documents and Settings\\Administrateur\\Mes documents\\MyApplication\\MyApplication\\CrystalReport1.rpt"); crConnectionInfo.ServerName = ".\\SQLEXPRESS"; crConnectionInfo.DatabaseName = "database"; crConnectionInfo.UserID = ""; crConnectionInfo.Password = ""; crConnectionInfo.IntegratedSecurity = true; CrTables = cryRpt.Database.Tables; foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables) { crtableLogoninfo = CrTable.LogOnInfo; crtableLogoninfo.ConnectionInfo = crConnectionInfo; CrTable.ApplyLogOnInfo(crtableLogoninfo); } cryRpt.SetDatabaseLogon("", "", ".\\SQLEXPRESS", "database"); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); }
But when i run the application, a login screen appeared and demands the connection Id and password, i tried to enter null values but the connection is failed.
Where is the problem??
回答1:
You have to initialize an instance of the DataSet class, and fill it with information from
your DataSet because Crystal report Datasource is based on a dataset. This is a basic code of
using Crystal report with Dataset:
SqlConnection con = new SqlConnection(); con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=YOUR PATH\database.mdf;Integrated Security=True;User Instance=True"; con.Open(); string sql = "SELECT * FROM tablename"; SqlDataAdapter dscmd = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); dscmd.Fill(ds, "tablename"); con.Close(); CrystalReport1 objRpt = new CrystalReport1(); objRpt.SetDataSource(ds.Tables["tablename"]); crystalReportViewer1.ReportSource = objRpt; crystalReportViewer1.Refresh();
回答2:
while we are creating the crystal report using windows explorer we have to provide the location of database ex: "C:\data" then once after that it works perfect but if we change the location of database file or the location of application it asks for login. The refresh of the datasource may also resolve your problem
consider this exapmle and try to resolve your problems here
回答3:
First goto database expert and check right side whether there is a single connection.if one or more connections there delete unwanted connection like access connections then copy and paste the below code it works perfectly
ReportDocument cryRpt = new ReportDocument(); TableLogOnInfos crtableLogoninfos = new TableLogOnInfos(); TableLogOnInfo crtableLogoninfo = new TableLogOnInfo(); ConnectionInfo crConnectionInfo = new ConnectionInfo(); Tables CrTables; string path = "" + Application.StartupPath + "\\Mgc\\Invoice.rpt"; cryRpt.Load(path); cryRpt.SetParameterValue("billno", Program.billno); crConnectionInfo.UserID = "userid"; crConnectionInfo.Password = "Password"; crConnectionInfo.ServerName = "servernme"; crConnectionInfo.DatabaseName = "database; CrTables = cryRpt.Database.Tables; foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables) { crtableLogoninfo = CrTable.LogOnInfo; crtableLogoninfo.ConnectionInfo = crConnectionInfo; CrTable.ApplyLogOnInfo(crtableLogoninfo); } crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh();