我有一个名为 DUI_Database 的数据库。里面是一个名为 DUI_Cite_Numbers 的表。其中有两列-dui_cite_no 和状态。我有一些代码从两个文本框中获取一系列数字并将它们放在表格中。此外,状态列是 FREE 或 ALLOCATED。ALLOCATED 号码已使用且不能重复使用。免费就是可用的号码。
如果您查看表格,它看起来像这样,假设我输入了从 D100100 到 D100105 的范围
DB Name: DUI_Database
Table Name: DUI_Cite_Numbers
dui_cite_no status
D100100 FREE
D100101 FREE
D100102 FREE
D100103 FREE
D100104 FREE
D100105 FREE
然后一旦这些数字被使用,它看起来像这样-
dui_cite_no status
D100100 ALLOCATED
D100101 ALLOCATED
D100102 ALLOCATED
D100103 FREE
D100104 FREE
D100105 FREE
我需要有关 SELECT 语句的帮助。当表单打开时,我需要在该表单的文本框中使用下一个 FREE dui_cite_no。我不确定如何做到这一点。
我以为这行得通-
"SELECT TOP(1) dui_cite_no FROM DUI_Cite_Numbers WHERE status = 'FREE'"
它不工作-看起来对吗?
因为一旦使用它,它就会在列中将其标记为 ALLOCATED -
"UPDATE DUI_Cite_Numbers SET status = 'ALLOCATED' WHERE dui_cite_no = @nextcit"
我很困惑,可能是因为我已经开始这样做太久了。
这是整个代码块:
Public Function Get_Next_DUI_Cit_Number() As String
''Get the next available citation number from the database. If there is no free
''citation number then return a null string
Dim nextcit As String = String.Empty
Using DataConnection As New System.Data.SqlServerCe.SqlCeConnection("Data Source=C:\Program Files\DailyLog DUI\DUI_Database.sdf")
DataConnection.Open()
Dim SelectCommand As New System.Data.SqlServerCe.SqlCeCommand("SELECT TOP(1) dui_cite_no FROM DUI_Cite_Numbers WHERE status = 'FREE'", DataConnection)
Dim DataReader As System.Data.SqlServerCe.SqlCeDataReader = SelectCommand.ExecuteReader()
If DataReader.Read() Then
nextcit = DataReader("dui_cite_no").ToString
Using DataConnection2 As New System.Data.SqlServerCe.SqlCeConnection("Data Source=C:\Program Files\DailyLog DUI\DUI_Database.sdf")
DataConnection2.Open()
Dim UpdateCommand As New System.Data.SqlServerCe.SqlCeCommand("UPDATE DUI_Cite_Numbers SET status = 'ALLOCATED' WHERE dui_cite_no = @nextcit", DataConnection2)
UpdateCommand.Parameters.AddWithValue("@nextcit", nextcit)
UpdateCommand.ExecuteNonQuery()
UpdateCommand.Dispose()
DataConnection2.Close()
End Using
End If
SelectCommand.Dispose()
DataConnection.Close()
End Using
End Function
然后当相应的表单打开时,代码如下:
Public Sub frmDUI_Citation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 MyBase.Load
Me.txt_dui_cite_no.Text = module1.Get_Next_DUI_Cit_Number() 'get the next available citation number and put it in the textbox
结束子