VB数据库中如何删除指定字段下的数据?

来源:百度知道 编辑:UC知道 时间:2024/05/16 06:38:08
我用的是Adodc1控件连接数据库,数据库有表:星期1、星期2——星期7.每个表中都有字段“开机时间1,关机时间1,状态1;开机时间2,关机时间2,状态2;。。。。,状态5”。现在我想删除指定字段的数据,比如开机时间1、关机时间1、状态1 这3个字段下的所有数据,然后写入新数据,但是结果总是把所有的字段下的数据全部都删除了,请问是怎么回事?给出代码最好,谢谢!

我的代码:

Adodc1.RecordSource = "Select 开机时间1,关机时间1,状态1 From 星期1"
Adodc1.Refresh
If Adodc1.Recordset.EOF = False Then
Adodc1.Recordset.Delete '删除原来记录
Adodc1.Recordset.Update
Adodc1.Refresh
End If
Adodc1.Recordset.AddNew '添加新记录
Adodc1.Recordset("开机时间1") = Text1(2).Text
Adodc1.Recordset("关机时间1") = Text1(3).Text
Adodc1.Recordset("状态1") = "关 闭"
Adodc1.Recordset.Update '更新数据

代码如上所示。请给指点指点,谢谢!

您所说的要求,其实不是删除,而是叫更新,就是把一批记录中的某些字段值全部更新为空值,请注意两者的区别.

在你的代码上改的话如下
Adodc1.RecordSource = "Select 开机时间1,关机时间1,状态1 From 星期1"
Adodc1.Refresh
'If Adodc1.Recordset.EOF = False Then
do while Adodc1.Recordset.EOF = False
'Adodc1.Recordset.Delete '删除原来记录
Adodc1.Recordset.Fields("开机时间1")=Text1(2).Text
Adodc1.Recordset.Fields("关机时间1")=Text1(3).Text
Adodc1.Recordset.Fields("状态1")="关 闭"
Adodc1.Recordset.Update
loop

Adodc1.Refresh
'End If
'Adodc1.Recordset.AddNew '添加新记录
'Adodc1.Recordset("开机时间1") = Text1(2).Text
'Adodc1.Recordset("关机时间1") = Text1(3).Text
'Adodc1.Recordset("状态1") = "关 闭"
'Adodc1.Recordset.Update '更新数据

这是修改数据的操作方法呀