查询每次运行都返回相同的数据
Posted
技术标签:
【中文标题】查询每次运行都返回相同的数据【英文标题】:Query returns same data every time it runs 【发布时间】:2016-04-10 12:18:58 【问题描述】:背景:
通过这些代码,我将不同的上诉号码添加到数据库中。每个上诉号码都有一个通过这些代码分配给它的分配代码。
问题:
问题是当我同时添加超过 2 个上诉时,此代码仅检测分配给添加的第一个上诉的分配代码,但不检测下一个添加的分配编号。
我尝试添加 Thread.sleep 方法,但结果是一样的。
Try
' Find out last assignment code
Dim fillCommand As String
Dim FillList As New OleDb.OleDbDataAdapter
Dim dt As New DataTable
Dim rowdata As Integer
Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ComplianceDB.accdb;Jet OLEDB:Database Password=xxxxxxx;" 'D:\Important Programing links\Programming Learning\Compliance Diary\ComlianceDiary\ComlianceDiary\database\ComplianceDB.accdb"
Dim Conn As New OleDb.OleDbConnection(ConnString)
Conn.Open()
If ListView1.Items.Count = 0 Then
MessageBox.Show("Please enter valid information to add appeal.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf BenchComboBox.Text = "" Or NoticeLocationTextBox.Text = "" Or BenchMembersTextBox.Text = "" Then
MessageBox.Show("Please enter all the required information to add appeal.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
For i = 0 To ListView1.Items.Count - 1
'Add Appeals to ATIR list
Me.ATIRTableAdapter.Insert(ClientComboBox.Text, ListView1.Items(i).SubItems.Item(1).Text, ListView1.Items(i).Text, ListView1.Items(i).SubItems.Item(2).Text, BenchComboBox.Text, BenchMembersTextBox.Text, HearingDate.Value.Date, ListView1.Items(i).SubItems.Item(4).Text, mgrComboBox.Text, txtdescription.Text, ListView1.Items(i).SubItems.Item(3).Text, NoticeLocationTextBox.Text, ChangePassword.GUIDTextBox.Text, DateAndTime.Today)
Dim AssignmentCommand = New OleDbCommand("select AssignmentCode, AssignmentName from Assignment where AssignmentName = 'ATIR Appeal - " & ListView1.Items(i).Text & "' AND ClientName = '" & ClientComboBox.Text & "'", Conn) ' (select max(AssignmentCode) from Assignment)" 'SELECT * FROM Assignment Where AssignmentName = '" & AssignmentNameTextBox.Text & "' AND ClientName = '" & ClientNameComboBox.Text & "'" ' WHERE DateofHearing BETWEEN " & Today.Date & " AND " & Today.Date.AddDays(15)
Dim Reader As OleDbDataReader = AssignmentCommand.ExecuteReader
'Check if the assignment name already exists, if yes, fetch old assignment code and add it to new compliance
If Reader.Read Then
Dim ATIRCodeCmd = New OleDbCommand("select ID from ATIR where ID = (select max(ID) from ATIR)", Conn) 'SELECT * FROM Assignment Where AssignmentName = '" & AssignmentNameTextBox.Text & "' AND ClientName = '" & ClientNameComboBox.Text & "'" ' WHERE DateofHearing BETWEEN " & Today.Date & " AND " & Today.Date.AddDays(15)
Dim ReaderATIRCode As OleDbDataReader = ATIRCodeCmd.ExecuteReader
While ReaderATIRCode.Read()
Me.NoticeComplianceTableAdapter1.Insert(ClientComboBox.Text, Reader.Item(1).ToString, Today.Date, Today.Date, Reader.Item(1).ToString, NoticeLocationTextBox.Text, txtdescription.Text, Reader.Item(0).ToString, "No", "", "", "Pending", ListView1.Items(i).SubItems.Item(1).Text, "", "", "", "Fresh Notice", "", "", ChangePassword.GUIDTextBox.Text, DateAndTime.Today, mgrComboBox.Text, PartnerNameTextBox.Text, SrManagerNameTextBox.Text, "AppellateCompliance", HearingDate.Value.Date, "", "", "", "", "", ReaderATIRCode.Item(0).ToString, "ATIR")
End While
Else
'if assignment does not exists, then create a new one and give new assignment number to new compliance.
Me.AssignmentTableAdapter1.Insert(ListView1.Items(i).SubItems.Item(1).Text, ClientComboBox.Text, "ATIR Appeal - " & ListView1.Items(i).Text, "N/A", "Pending", "No", Today.Date, HearingDate.Value.Date, HearingDate.Value.Date, mgrComboBox.Text, SrManagerNameTextBox.Text, PartnerNameTextBox.Text, "Income Tax Ordinance, 2001", "Pending", "", "Pending", "", "Pending", "", Today.Date, "", Today.Date, "Pending", Today.Date, "")
Dim AssignmentCodeCmd = New OleDbCommand("select AssignmentCode from Assignment where AssignmentCode = (select max(AssignmentCode) from Assignment)", Conn) 'SELECT * FROM Assignment Where AssignmentName = '" & AssignmentNameTextBox.Text & "' AND ClientName = '" & ClientNameComboBox.Text & "'" ' WHERE DateofHearing BETWEEN " & Today.Date & " AND " & Today.Date.AddDays(15)
Dim ReaderCode As OleDbDataReader = AssignmentCodeCmd.ExecuteReader
While ReaderCode.Read()
rowdata = ReaderCode.Item(0).ToString
ReaderCode.NextResult()
End While
Dim ATIRCodeCmd = New OleDbCommand("select ID from ATIR where ID = (select max(ID) from ATIR)", Conn) 'SELECT * FROM Assignment Where AssignmentName = '" & AssignmentNameTextBox.Text & "' AND ClientName = '" & ClientNameComboBox.Text & "'" ' WHERE DateofHearing BETWEEN " & Today.Date & " AND " & Today.Date.AddDays(15)
'Thread.Sleep(1000)
Dim ReaderATIRCode As OleDbDataReader = ATIRCodeCmd.ExecuteReader
While ReaderATIRCode.Read()
Me.NoticeComplianceTableAdapter1.Insert(ClientComboBox.Text, "ATIR Appeal - " & ListView1.Items(i).Text, Today.Date, Today.Date, "ATIR Appeal - " & ListView1.Items(i).Text, NoticeLocationTextBox.Text, txtdescription.Text, rowdata, "No", "", "", "Pending", ListView1.Items(i).SubItems.Item(1).Text, "", "", "", "Fresh Notice", "", "", ChangePassword.GUIDTextBox.Text, DateAndTime.Today, mgrComboBox.Text, PartnerNameTextBox.Text, SrManagerNameTextBox.Text, "AppellateCompliance", HearingDate.Value.Date, "", "", "", "", "", ReaderATIRCode.Item(0).ToString, "ATIR")
End While
End If
Reader.Close()
Next
MessageBox.Show("ATIR Appeal(s) have been noted.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
Conn.Close()
Me.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
【问题讨论】:
NEVER EVER 执行SELECT MAX(...) + 1
之类的操作来获得“下一个最高”的 ID 号 - 这只是 NOT在多用户系统中安全!让数据库处理这个问题——只需对这些类型的计数器使用自动编号——MS Access 中的 Autonumber
或 SQL Server 中的 INT IDENTITY
列。不要试图自己动手——只是不要——使用数据库给你的东西!
请建议我如何实现我的目的。 Select MAX(..) 用于从数据库中的另一个表中获取下一个最高 ID 值。哪个命令更适合从另一个表中获取最高值。
我已经告诉过你:使用数据库提供的工具 - MS Access 中的autonumber
,SQL Server 中的INT IDENTITY
....
还要注意: SQL Injection alert - 您应该不将您的 SQL 语句连接在一起 - 改用 参数化查询避免 SQL 注入
autonumber
用于在同一个表中生成数字。但是,在我的代码中,我必须从另一个表中获取分配代码,以便在NoticeCompliance
表中输入相同的代码。
【参考方案1】:
没关系。我通过使用@@IDENTITY
sql 命令解决了我的问题。感谢大家的贡献。
【讨论】:
以上是关于查询每次运行都返回相同的数据的主要内容,如果未能解决你的问题,请参考以下文章
我想从 C# 中的选择查询中获取 id 但每次我运行程序时,查询都会返回“-1” [重复]