求解一个DeleteCommand为啥不能删除数据
求解一个DeleteCommand为什么不能删除数据
如题上代码:
以上代码不能删除对应的数据库里的记录,请高手指点。
------解决方案--------------------
oracle中参数本身就是: 上面是没有问题的
真正的问题在于,你所填充的DataTable 是没有任何更新的,因此是不会最终执行删除操作的
因此在da.Update(dt); 之前你要保证你的DataTable 是有删除操作的
如下
如题上代码:
- C# code
using (OracleDataAdapter da = new OracleDataAdapter()) { try { string strSQL="Select * from t1"; OpenConn();//打开连接 OracleCommand cmd; OracleParameter parm; DataTable dt = new DataTable(); cmd = new OracleCommand(strSQL, conn); da.SelectCommand = cmd; da.Fill(dt); string delsql = "DELETE FROM t1 WHERE ID=:pID"; cmd = new OracleCommand(delsql, conn); parm = cmd.Parameters.Add(":pID", OracleType.VarChar, 50, "ID"); parm.SourceVersion = DataRowVersion.Original; da.DeleteCommand = cmd; // int a = da.Update(dt); } catch (Exception ex) { //省略 } }
以上代码不能删除对应的数据库里的记录,请高手指点。
------解决方案--------------------
oracle中参数本身就是: 上面是没有问题的
真正的问题在于,你所填充的DataTable 是没有任何更新的,因此是不会最终执行删除操作的
因此在da.Update(dt); 之前你要保证你的DataTable 是有删除操作的
如下
- C# code
adapter.Fill(dt); dt.Rows[1].Delete();//删除DataTable中的某一行,根据需要自行删除 adapter.DeleteCommand = com; adapter.Update(dt);