1 private void 洱海各月出流流量frm_Load(object sender, EventArgs e) 2 { 3 //在查询按钮中用下面这段代码会出现打开数据库出错的现象 4 MyTable1.Rows.Clear(); 5 //string msql = "Select 年 as Year order by 年 asc,全年平均 as YearAvr into YearAvrLL from 洱海各月出流流量";//新建表 6 7 string MySQL = "Select * from 洱海各月出流流量 order by 年 asc"; 8 9 OleDbConnection MyConnection = new OleDbConnection(MyConnectionString); 10 11 MyConnection.Open(); 12 13 OleDbDataAdapter MyAdatper = new OleDbDataAdapter(MySQL, MyConnection); 14 //OleDbDataAdapter MyAdatper1 = new OleDbDataAdapter(msql, MyConnection); 15 16 MyAdatper.Fill(MyTable1); 17 18 this.dataGridView1.DataSource = MyTable1; 19 20 if (MyConnection.State == ConnectionState.Open) 21 { 22 MyConnection.Close(); 23 } 24 25 }
1 public string MyConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ErLake.mdb"; 2 3 public System.Data.DataTable MyTable1 = new System.Data.DataTable(); 4 5 private void 查询_Click(object sender, EventArgs e) 6 { 7 MyTable1.Rows.Clear(); 8 string nyr1 = dateTimePicker1.Value.Date.ToString("yyyy-MM-dd");//yyyy-MM-dd正确提取和转换格式,用yyyy-mm-dd格式提取时月份会出错 9 string[] tqn1 = nyr1.Split(‘-‘); //将提取到的yyyy-MM-dd,通过‘-’分割,存入数组 10 string nyr2 = dateTimePicker2.Value.Date.ToString("yyyy-MM-dd");//yyyy-MM-dd正确提取和转换格式,用yyyy-mm-dd格式提取时月份会出错 11 string[] tqn2 = nyr2.Split(‘-‘); //将提取到的yyyy-MM-dd,通过‘-’分割 12 13 string sql = "select * from 洱海各月出流流量 where 年 between ‘" + tqn1[0] + "‘ and ‘" + tqn2[0] + "‘ order by 年 asc";//通过修改数据库年字段的数据类型OK,数字类型改为文本类型 14 15 OleDbConnection myCon = new OleDbConnection(MyConnectionString); 16 OleDbDataAdapter DA = new OleDbDataAdapter(sql, myCon); 17 DA.Fill(MyTable1); 18 dataGridView1.DataSource = MyTable1; 19 20 21 22 23 }
窗体加载部分,解决查询结果不从第一行显示,而是不断往表下面扩展问题
现在的结果是:没查询一次,结果从第一行显示。
dataGridView1.DataSource,解决查询结果不从第一行显示,而是不断往表下面扩展问题,布布扣,bubuko.com