VB中如何用文本控件向ado控件和datagrid控件建立的表中增加数据

VB中如何用文本控件向ado控件和datagrid控件建立的表中增加数据,第1张

首先要把ADO连接到数据库,ado下有记录集(Recordset)对象,Recordset对象有Addnew方法,Addnew后,用

Ado.Recordset.fields("列名").value=值

然后

Ado.recordset.update

添加:

’下面这句连接数据库指定表的所有字段

Adodc1.RecordSource = "select * from 库存表"

’下面这句添加记录

Adodc1.Recordset.AddNew

下面4句就是通过4个TEXTBOX控件输入字段的数据,如果是数字类型可以加VAL

Adodc1.Recordset.Fields("货物编号") = Text1.Text

Adodc1.Recordset.Fields("货物名称") = Text2.Text

Adodc1.Recordset.Fields("库存量") = Text3.Text

Adodc1.Recordset.Fields("单位") = Text4.Text

’下面这个是修改后更新

Adodc1.Recordset.Update

修改:

'通过主键指定要修改的记录

Adodc1.RecordSource = "select * from 库存表 where 货物编号 = " &Text1.Text

Adodc1.Refresh

Adodc1.Recordset.Fields("货物编号") = Text1.Text

Adodc1.Recordset.Fields("货物名称") = Text2.Text

Adodc1.Recordset.Fields("库存量") = Text3.Text

Adodc1.Recordset.Fields("单位") = Text4.Text

1

你是不是用DATAGRID显示个recordset的~用addnew添加数据应该是可以直接更新的啊

2

用个recordset执行个统计的SQL语句后或者合计结果

比如用下面代码打开个recordset

select sum(num) as 合计 from books

rs.fields("合计")就是num这列的总合

然后在按照你程序要求计算

3

打开recordset的时候用别名

select username as 用户名 from usertable

这样就是显示中文列名了

4

Set cmdstr = New ADODB.Command

cmdstr.ActiveConnection = conn (ADODB.Connection的名)

cmdstr.CommandText = str_sql (SQL语句)

str_sql="update usertable set name='abc' where id=123"

cmdstr.CommandType = adCmdText

cmdstr.Execute (执行update usertable set name='abc' where id=123)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11723326.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存