如何用VB实现对sql 数据表的更新?

如何用VB实现对sql 数据表的更新?,第1张

你的创建一个数据库连接对象,如adodb库中command对象、recordset对象

使用command对象,必须要adodb.connection对象,详细语法查adodb帮助

可以使用

command.execute "update score set scores=scores+" &text2.text &" where number=" &text1.text

完成更新

附示例:

Public Sub ExecuteX()

Dim strSQLChange As String

Dim strSQLRestore As String

Dim strCnn As String

Dim cnn1 As ADODB.Connection

Dim cmdChange As ADODB.Command

Dim rstTitles As ADODB.Recordset

Dim errLoop As ADODB.Error

' Define two SQL statements to execute as command text.

strSQLChange = "UPDATE Titles SET Type = " &_

"'self_help' WHERE Type = 'psychology'"

strSQLRestore = "UPDATE Titles SET Type = " &_

"'psychology' WHERE Type = 'self_help'"

' Open connection.

strCnn = "Provider=sqloledb" &_

"Data Source=srvInitial Catalog=PubsUser Id=saPassword="

Set cnn1 = New ADODB.Connection

cnn1.Open strCnn

' Create command object.

Set cmdChange = New ADODB.Command

Set cmdChange.ActiveConnection = cnn1

cmdChange.CommandText = strSQLChange

' Open titles table.

Set rstTitles = New ADODB.Recordset

rstTitles.Open "titles", cnn1, , , adCmdTable

' Print report of original data.

Debug.Print _

"Data in Titles table before executing the query"

PrintOutput rstTitles

' Clear extraneous errors from the Errors collection.

cnn1.Errors.Clear

' Call the ExecuteCommand subroutine to execute cmdChange command.

ExecuteCommand cmdChange, rstTitles

' Print report of new data.

Debug.Print _

"Data in Titles table after executing the query"

PrintOutput rstTitles

' Use the Connection object's execute method to

' execute SQL statement to restore data. Trap for

' errors, checking the Errors collection if necessary.

On Error GoTo Err_Execute

cnn1.Execute strSQLRestore, , adExecuteNoRecords

On Error GoTo 0

' Retrieve the current data by requerying the recordset.

rstTitles.Requery

' Print report of restored data.

Debug.Print "Data after executing the query " &_

"to restore the original information"

PrintOutput rstTitles

rstTitles.Close

cnn1.Close

Exit Sub

Err_Execute:

' Notify user of any errors that result from

' executing the query.

If rstTitles.ActiveConnection.Errors.Count >= 0 Then

For Each errLoop In rstTitles.ActiveConnection.Errors

MsgBox "Error number: " &errLoop.Number &vbCr &_

errLoop.Description

Next errLoop

End If

Resume Next

End Sub

Public Sub ExecuteCommand(cmdTemp As ADODB.Command, _

rstTemp As ADODB.Recordset)

Dim errLoop As Error

' Run the specified Command object. Trap for

' errors, checking the Errors collection if necessary.

On Error GoTo Err_Execute

cmdTemp.Execute

On Error GoTo 0

' Retrieve the current data by requerying the recordset.

rstTemp.Requery

Exit Sub

Err_Execute:

' Notify user of any errors that result from

' executing the query.

If rstTemp.ActiveConnection.Errors.Count >0 Then

For Each errLoop In Errors

MsgBox "Error number: " &errLoop.Number &vbCr &_

errLoop.Description

Next errLoop

End If

Resume Next

End Sub

Public Sub PrintOutput(rstTemp As ADODB.Recordset)

' Enumerate Recordset.

Do While Not rstTemp.EOF

Debug.Print " " &rstTemp!Title &_

", " &rstTemp!Type

rstTemp.MoveNext

Loop

End Sub

不能直接用,因为数据库环境不同,稍微修改下就可以了,不过之前你最好看下ADODB手册,网上有。

是工程->引用中 Microsoft ActiveX Data Objects x.x Library

也可以使用工具栏中->部件中 Microsoft ADO DataControl x.x(OLEDB)

他们区别在于一个是控件,一个是函数库,若是熟悉点的人都用Library,添加DataControl时VB会自动添加Library,DataControl实际还是通过Library处理的。你要是不熟悉就用DataControl吧,有图形界面你可能容易上手些。QQ群2832109里有ADO帮助。

你的代码中conn.Open "Provider=SQLOLEDB.1Persist Security Info=FalseUser ID= sapassword=Initial Catalog=publicData Source=."

“Data Source” 要指定数据源

cnn.Execute 也可以执行命令你的命令只是个查询是不返回结果的。

你的sql语句,是将dsd表中所有记录的“单价/吨”字段内容都改掉了

用了ado控件,就没必要用 update的sql语句了

直接给ado控件的fields属性赋值就可以了

您好,很高兴为您解答。

你创建一个数据库连接对象,如adodb库中命令对象、recordset对象,使用命令对象,必须要adodb.connection对象,详细语法查adodb帮助。数据库环境不同,稍微修改下就可以了,不过之前你最好看下ADODB手册,网上有。是工程->引用中 Microsoft ActiveX Data Objects x.x Library。也可以使用工具栏中->部件中 Microsoft ADO DataControl x.x

你的代码中conn.打开"Provider=SQLOLEDB.1保留安全信息 =假用户 ID= sapassword=初始目录=公共数据源=.""Data Source" 要指定数据源CNN.Execute 也可以执行命令 可你的命令只是个查询是不返回结果的。

如果觉得合适,请采纳我的回答。


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

原文地址: http://outofmemory.cn/sjk/6670927.html

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

发表评论

登录后才能评论

评论列表(0条)

保存