我是 vb 和 mysql 的初学者...我正在从事一个项目(vb 2010 express 和 mysql5.1),其中局域网中有两台机器。应用程序安装在两台机器上,数据库在其中一台机器上。两者都使用相同的用户(dr_admin)连接到数据库。我的问题是一台机器所做的更改不会反射(reflect)在另一台机器上。
DBCon = New ADODB.Connection
DBCon.ConnectionString = ("Driver={MySQL ODBC 3.51 Driver};Server=Admin-PC; Database=dr_db; User=dr_admin;Password=dradmin;pooling=false;Option=3;")
DBCon.Open()
MsgBox(DBCon.State)
Try
Cmd = New ADODB.Command
Cmd.ActiveConnection = DBCon
Cmd.CommandText = "SEt autocommit=0;"
Cmd.Execute()
这就是我连接的方式,并在需要时手动提交和回滚。
我从数据库中填充一个列表。该列表显示在面板上。每次面板可见时都会加载数据。 但是新数据没有显示。 然后我添加了一些代码来关闭当前的数据库连接并再次重新打开。
Public Sub reconnect_pr()
DBCon.Close()
MsgBox(DBCon.State)
DBCon = Nothing
DBCon = New ADODB.Connection
DBCon.ConnectionString = ("Driver={MySQL ODBC 3.51 Driver};Server=Admin-PC;Database=dr_db; User=dr_admin;Password=dradmin;pooling=false;Option=3;")
DBCon.Open()
MsgBox(DBCon.State)
Try
Cmd = New ADODB.Command
Cmd.ActiveConnection = DBCon
Cmd.CommandText = "SEt autocommit=0;"
Cmd.Execute()
Catch ex As Exception
MsgBox("autocommit error - Contact admin", MsgBoxStyle.OkOnly, "Patient Care")
End Try
End Sub
这是我用来从数据库中检索数据的代码。
Cmd = New ADODB.Command
Cmd.ActiveConnection = login_frm.DBCon
Cmd.CommandText = "select pat_no,pat_id,pat_name from pat_token_details order by pat_no;"
Rs = Cmd.Execute
exp_tod_pat_list.Items.Clear()
exp_tod_pat_list.Items.Add("None")
Do While Not Rs.EOF
v_token = Rs("pat_no").Value
v_pat_name = Rs("pat_name").Value
v_id = Rs("pat_id").Value
exp_tod_pat_list.Items.Add("Token id: " + v_token + " - " + v_pat_name + " ( " + v_id + " )")
Rs.MoveNext()
Loop
Catch ex As Exception
MsgBox("unable to fetch current token details", MsgBoxStyle.OkOnly, "Token")
End Try
它仍然不起作用..但是当我关闭应用程序并再次打开它时它会反射(reflect)出来。
请帮助我理解问题。
谢谢。
最佳答案
此行为原因的简短解释:
在 MySql 中,默认隔离级别是可重复读取。
在这种模式下(详见此链接:click me):
All consistent reads within the same transaction read the snapshot established by the first read. This convention means that if you issue several plain (nonlocking) SELECT statements within the same transaction, these SELECT statements are consistent also with respect to each other.
简单来说:如果您在同一个事务中多次发出相同的 SELECT 语句,您将总是得到相同的结果,无论数据库如何变化由其他 session 制作。
session 会记住,事务中的第一个 SELECT 在时间 X 运行并在时间 X“制作数据快照”,然后所有后续选择将在时间 X 读取此快照,但不读取当前数据。
查看下一个链接:(click me):
If the transaction isolation level is REPEATABLE READ (the default level), all consistent reads within the same transaction read the snapshot established by the first such read in that transaction. You can get a fresher snapshot for your queries by committing the current transaction and after that issuing new queries.
通过提交事务(或发出事务回滚),您结束了该事务,下一个 SELECT(在新事务中)将看到新数据。
如果启用 auto_commit,那么所有选择总是自动提交 - 所以你总是看到新数据。
但是如果你禁用自动提交,在可重复读取(默认)隔离级别,你必须手动提交才能看到更改数据库。
注意:以上行为仅适用于 InnoDb 表。其他引擎不支持 ACID。
关于mysql - 在 mysql 数据库中所做的更改不会反射(reflect)在 vb.net 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21973684/