For(int i=0;i dataGridView1.Columns[i].AutoSizeMode=DataGridViewAutoSizeColumnMode.AllCells;//自适应列宽 dataGridView1.Columns[0].HeaderText="列名", 设置表的列名 } 1,连接数据库字符串; 2连接对象 SqlConnection con=new SqlConnection(连接数据库的字符串) 3,打开连接 4,数据库语句 5,创建数据库适配器对象,用于填充dataset 对象 SqlDataAdapter da=new SqlDataAdapter(sql,con); 传入的参试sql语句,和一个数据库连接对象。 6,创建Dataset 对象 DataSet ds=new DataSet() 7填充 da.Fill(ds,"fsdf随便填"); 8关闭连接 9为数据组件绑定数据源 dataGridView1.DataSource=ds.Ta Con.Open() String sql="select * from 表名"; SqlCommand comm=new SqlCommand(sql, con); SqlDataAdapter dr=new SqlDataAdapter(); Dr.SelectCommand=comm; DataSet ds=new DataSet(); Dr.Fill(ds); Con.Close(); dataGridView1.DataSource=ds.Tables[0].DefaultView; string sql=“update 表名 set 字段名=das where 。。。” 更新,插入,删除用SqlCommand 对象,表示:执行一个语句或一个存储过程 SqlCommand cmd=new SqlCommand(sql,con) Cmd.ExecuteNonQuery(); String sql=“select count(*) from 表名 where id=。。” SqlCommand com=new SqlCommand(sql,connection); String line=com.ExecuteScalar().ToString(); 返回;影响的行数。 DialogResult r = MessageBox.Show("确认要删除该执法人员的信息吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk); If(r==DialogResult.Cancel) Sql 中时间类型的字段可以时间格式相同的字符串的相比较 先新建这个列对象 System.Windows.Forms.DataGridViewCheckboxColumn chk; Chk=new System.Windows.Forms.DataGridViewCheckBoxColumn(); Chk.HeaderText="选择"; chkName=“chk”; This.dataGridView1Columns.Add(chk); DataGridViewCheckBoxCell checkCell; private void button2_Click(object sender, EventArgs e) { try { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn("f_vehicleid",typeof(string)), new DataColumn("f_axlesum",typeof(string)), new DataColumn("f_weightlimited",typeof(string)), new DataColumn("f_fctime",typeof(string)), new DataColumn("f_checkno",typeof(string)), new DataColumn("f_fctotalweight",typeof(string)), new DataColumn("f_overlimited",typeof(string)), new DataColumn("f_Rctime",typeof(string)), new DataColumn("f_rctotalweight",typeof(string)), new DataColumn("f_offload",typeof(string)), new DataColumn("f_model1",typeof(string)), new DataColumn("f_carTel",typeof(string)), }); for (int i = 0; i < dataGridView1.Rows.Count; i++) { checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["Chk"]; if (Convert.ToBoolean(checkCell.Value) == true) { DataRow dr = null; dr = dt.NewRow(); // dr["id"] = i; //将选中的一行数据转换到datatable中 //dr["id"] = Convert.ToString(dataGridView1.Rows[i].Cells["id"].Value); dr["f_vehicleid"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_vehicleid"].Value); dr["f_axlesum"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_axlesum"].Value); dr["f_weightlimited"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_weightlimited"].Value); dr["f_fctime"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_fctime"].Value); dr["f_checkno"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_checkno"].Value); dr["f_fctotalweight"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_fctotalweight"].Value); dr["f_overlimited"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_overlimited"].Value); dr["f_Rctime"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_Rctime"].Value); dr["f_rctotalweight"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_rctotalweight"].Value); dr["f_offload"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_offload"].Value); dr["f_model1"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_model1"].Value); dr["f_carTel"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_carTel"].Value); dt.Rows.Add(dr); // LoginUser bll = new LoginUser(); string id = dr["id"].ToString(); // bll.UptIsExcel(id) } }//for private void button1_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Excel files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.Title = "导出Excel文件到"; saveFileDialog.ShowDialog(); try { Stream myStream; myStream = saveFileDialog.OpenFile(); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312")); string str = ""; //写标题 for (int i = 0; i < dataGridView1.ColumnCount; i++) { if (i > 1) { str += "\t"; } str += dataGridView1.Columns[i].HeaderText; } sw.WriteLine(str); //写内容 for (int j = 0; j < dataGridView1.Rows.Count; j++) { string tempStr = ""; for (int k = 0; k < dataGridView1.Columns.Count; k++) { if (k > 0) { tempStr += "\t"; } tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString(); } sw.WriteLine(tempStr); } MessageBox.Show("导出成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); sw.Close(); myStream.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { //sw.Close(); //myStream.Close(); } } String s=“Select * from 表名 where dateTime>='”+DateTime.Now.AddDays(-7).Tostring()+"' and dateTime<='"+DateTime.Now.ToLocalTime().ToString()+"' order by dateTime desc"; DateTime dt=this.dateTimePicker1.Value; String sql="select * from 表名 where dateTiem>='"+dt.AddDays(-7).ToString()+"' and dateTiem<="+this.dateTimePicker1.Value.Tostring()+"' order by [dateTime] desc"; select f_site_code,f_site_name,f_road_Coding,mileage,meter, count(f_hspid) totalNum, sum(CASE WHEN f_overlimited > 1000 THEN 1 ELSE 0 END ) AS overNum, 0 as cxl from b_site a left join b_hspinfo h on a.f_site_code = h.f_stationid where a.f_type='1' group by f_site_code,f_site_name,f_road_Coding,mileage,meter; select (case when f_speed is null then 12 else f_speed end ) as f_speed from b_hspinfo; Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource(); reportViewer1.Reset(); reportViewer1.LocalReport.DataSources.Clear(); this.reportViewer1.LocalReport.ReportPath = @"..\..\RepNoStopCar.rdlc"; reportViewer1.LocalReport.ReportEmbeddedResource = "winFmain.Fm_RptNoStopCar.RepNoStopCar.rdlc"; reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dt)); this.reportViewer1.RefreshReport();连接数据库
Select 查询的另外一种方式
更新数据库语句
如果select 查询的只是单个影响的行数,
dataGridView 组件新添加一个checkBox的列
//判断勾选的代码,并获取勾选之后的选中的单元的数据。
//导出excel的代码
获取当前时间并在过去一周时间的查询
获取时间控件的时间并在其过去一周的查询
数据库case when 操作
窗体设置打印报表代码
行转列操作。。