以下是我写的ADO.NET程序,可是并没有更新数据库,请问这是为什么

brucerong 2009-04-30 10:45:22
今天我做了几道关于ADO.NET的练习题,有一个程序编译不出错,但是更新不到数据库中,请问这是为什么?该如何改正?下面是出问题的函数:
我想实现的功能是:在非连接状态下,删除一行,然后UPDATE到数据库中。
代码如下:

static void UseDataDelete()
{
String conStr = "server=.;database=Students;Integrated Security=true";
SqlConnection conn = new SqlConnection(conStr);
String sqlSel = "select * from major";
SqlDataAdapter adp = new SqlDataAdapter(sqlSel,conn);
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adp);
DataSet ds = new DataSet();
adp.Fill(ds);
DataTable dt = ds.Tables[0];
dt.Rows.Remove(dt.Rows[1]);
adp.Update(dt);
conn.Close();
}
谢谢!
...全文
60 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kongdelu2008 2009-05-01
  • 打赏
  • 举报
回复
可以以下中的一个删除行;

ds.Tables[0].Rows[0].Delete();

ds.Tables[0].Rows.RemoveAt(0);
qiweihero 2009-05-01
  • 打赏
  • 举报
回复
主键
zjh222 2009-05-01
  • 打赏
  • 举报
回复
一。你看看,你的表是不是没有设主键呢??
二。要加AcceptChange()方法
Mr_Long 2009-04-30
  • 打赏
  • 举报
回复



dt.Rows.Remove(); 这个方法里的参数是数据行DataRow类型的。
dt.Rows.RemovAt(); 这里才的参数才是行的索引。
蓝海D鱼 2009-04-30
  • 打赏
  • 举报
回复

dt.Rows.Remove(dt.Rows[1]);
改成
dt.Rows.RemoveAt(1);

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧