首页 新闻 搜索 专区 学院

C#导入EXCEL到数据库,用OLE连接EXCEL,并创建datatable,但是对datatable进行delete操作时,总是出现与预期

0
[已关闭问题] 关闭于 2015-06-02 15:40

C#导入EXCEL到数据库,用OLE连接EXCEL,并创建datatable,但是对datatable进行delete操作时,总是出现与预期结果不符合的情况

问题补充:

DataSet ds = new DataSet(); DataTable dt1 = new DataTable(); DataTable dt2 = new DataTable(); DataTable dt3 = new DataTable(); dt2.Columns.Add("工号"); dt2.Columns.Add("时间"); string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; " + "Data Source =" + txtPath .Text + ";Extended Properties=Excel 8.0"; OleDbConnection myConn = new OleDbConnection(strCon); string sql = "select * from [sheet1$A:B]"; int kk = 0; try { myConn.Open(); OleDbDataAdapter oda = new OleDbDataAdapter(sql, myConn); oda.Fill(ds, "[Sheet1$]"); myConn.Close(); dt1 = ds.Tables[0]; for (int i = 0; i < dt1.Rows.Count; i++) { DataRow dr = dt2.NewRow(); dt2.Rows.Add(dr); dt2.Rows[i]["工号"] = dt1.Rows[i][0]; dt2.Rows[i]["时间"] = dt1.Rows[i][1]; } DataRowCollection rows = dt2.Rows; for (int j = 0; j < dt2.Rows.Count; j++) { if (Convert.ToInt32(dt2.Rows[j]["工号"]) == 1001) { rows[j].Delete(); } } dt2.AcceptChanges(); label1.Text = dt2.Rows.Count.ToString(); //label1.Text = kk.ToString(); this.dataGridView1.DataSource = dt2; dataGridView1.Columns[1].Width = 200; } catch (Exception ex) { MessageBox.Show(ex.Message); }

缪健的主页 缪健 | 菜鸟二级 | 园豆:214
提问于:2015-06-02 15:12
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册