如何在vb中实现一个变量的数据不断变化,然后把这个变量的值实时传个数据库

如何在vb中实现一个变量的数据不断变化,然后把这个变量的值实时传个数据库,第1张

变量不断变化可以用rnd函数

Dim dblReturn As Double

Randomize Now

dblReturn = Rnd

MsgBox dblReturn

得到的dblReturn是一个0~1的随机数

在timer控件中写代码进行保存,timer的时间间隔设定为数据变化的时间。

VB可通过结构化查询语言(SQL)访问数据库

下面帮你写一个简单的过程,将学号以参数传给该过程:

Private Sub editmark(num as Integer)

'声明一个记录集

Dim rs As New ADODB.Recordset

'定义一个SQL查询串

SQLStr = "select * from student where id=" &num

'执行查询

rs.Open SQLStr, Conn, 3, 2

If Not rs.EOF Then

rs("mark")=rs("mark")-10

end if

'更新数据库

rs.update

End Sub

--------------------------------------------------------------

补充:

1、对于Conn提问者肯定明白,因为提问者已经说明连接部分可以忽略;

2、对于rs("mark")=rs("mark")-10 是可行的,因为通过过程参数的传递记录已经定位在某条记录上,还考虑什么转移;

3、我们回答提问者的问题应尽量简单可行,说明问题即可,无需长篇大论让提问者一头雾水,长篇大论就是“敬业”吗?

--------------------------------------------------------------

最后补充:

没有关系,我们是在讨论问题嘛!

其实,对于“vb *** 作数据库”我相信不是我们在这里举一个简单的例子能说透的。我只是想通过这个简单的例子(越简单越易让提问者搞懂),告诉提问者用SQL可以访问并 *** 作数据库,至于具体许多细节需要提问者熟悉一下SQL和VB提供的ADO,我想这也是提问者的真实意图。

另外,关于rs("mark")=rs("mark")-10在这个例子中肯定是可行的,因为学生的id肯定是唯一的(应该是数据表student的主键),不会有重复的id。

你的创建一个数据库连接对象,如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 也可以执行命令 可你的命令只是个查询是不返回结果的。


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

原文地址:https://54852.com/sjk/9537945.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存