private void setCombo(string sql, ComboBox cmb) { //清空下拉列表 cmb.Items.Clear(); cmb.Items.Add("%"); cmb.Text = "%"; //实例化一个类 getLxerpConn v = new getLxerpConn(); //获取类里面的sql连接到数据库的字符串 v.getsqlconn(); // string connStr = "Data Source=.\\sql2008;Initial Catalog=books;Integrated Security=True"; //连接到数据库的字符串 SqlConnection cn = new SqlConnection(v.Sqlconn); //打开链接 cn.Open(); SqlCommand cmd = cn.CreateCommand(); cmd.CommandText = sql; SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { cmb.Items.Add(dr.GetValue(0).ToString().Trim()); } cn.Close(); cn.Dispose(); }
public void baobiao(string cmdsql, string reportName) { circularProgress1.Visible = true; circularProgress1.IsRunning = true; getLxerpConn v = new getLxerpConn(); v.getsqlconn(); //声明连接、命令对象及其他相关对象 SqlConnection conReport = new SqlConnection(v.Sqlconn); SqlCommand cmdReport = new SqlCommand(); SqlDataReader drReport; DataSet dsReport = new dsQuan(); try { //打开连接 conReport.Open(); //准备连接对象以把获取的数据放入数据集 cmdReport.CommandType = CommandType.Text; cmdReport.Connection = conReport; cmdReport.CommandText = cmdsql; //从命令对象中读取数据 drReport = cmdReport.ExecuteReader(); //有了ADO.NET,可把读取来的数据直接加载到数据集中 dsReport.Tables[0].Load(drReport); //关闭读取及连接 drReport.Close(); conReport.Close(); //为查看器提供本地报表数据 reportViewer1.LocalReport.ReportEmbeddedResource = reportName; //准备报表数据源 ReportDataSource rds = new ReportDataSource(); rds.Name = "dsQuan"; rds.Value = dsReport.Tables[0]; //clear非常重要,不加没数据,坑死人 reportViewer1.LocalReport.DataSources.Clear(); reportViewer1.LocalReport.DataSources.Add(rds); ReportParameter canshu = new ReportParameter("F_date_to_date", "【从】" + DTbegin.Text + "【到】" + DTend.Text); this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { canshu }); //加载报表查看器 reportViewer1.RefreshReport(); } catch (Exception ex) { //显示错误信息 MessageBox.Show(ex.Message + "\n" + ex.StackTrace); } finally { //检查连接是否仍然打开,如果是,关闭它。 if (conReport.State == ConnectionState.Open) { conReport.Close(); } circularProgress1.IsRunning = false; circularProgress1.Visible = false; } }