2012-09-15 10 views
0

我有一個名爲DUI_Database的數據庫。裏面是一個名爲DUI_Cite_Numbers的表格。有兩列 - dui_cite_no和狀態。我有一些代碼需要兩個文本框中的一系列數字,並將它們放在表中。此外,狀態列是免費的或分配的。分配的號碼被使用並且不能被重新使用。免費只是可用的數字。要轉到下一個可用編號的SQL語句

如果你看了一下表,它是這樣的,假設我進入了一個範圍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語句。當表單打開時,我需要在該表單的文本框中使用下一個免費的dui_cite_no。我不知道如何做到這一點。

我認爲這將與工作

"SELECT TOP(1) dui_cite_no FROM DUI_Cite_Numbers WHERE status = 'FREE'" 

這不是working-不正確?

因爲一旦它的使用,它標誌着它爲分配在列 -

"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 

然後適當的形式打開時,這是代碼:

公用Sub frmDUI_Citation_Load(BYVAL發件人爲System.Object的,BYVALË作爲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 

結束子

+1

是你的數據庫MS SQL?當你說它不工作時,你有錯誤嗎? – greg84

+0

Sql CE。當我打開表單時,我沒有收到任何東西,這意味着我的文本框沒有填充。我試圖把這個值放在一個消息框中去查看,但我什麼也沒得到。至於錯誤,我的代碼似乎沒有錯誤。 我剛添加了我的整段代碼。是的,我打算使用連接字符串的中央位置... –

+0

您可以發佈您用於對數據庫運行查詢的代碼嗎? – greg84

回答

0

如果你想確保它們是按順序發佈的,你需要添加一個ORDER BY

SELECT TOP(1) 
    dui_cite_no 
FROM 
    DUI_Cite_Numbers 
WHERE 
    status = 'FREE' 
ORDER BY 
    dui_cite_Number 
+0

我想你的suggestion- ( 「SELECT TOP(1)從dui_cite_no WHERE DUI_Cite_Numbers狀態= '免費' ORDER BY dui_cite_no」,DataConnection) 我還添加了返回nextcit只是功能的結束,仍然沒有之前。 –

+0

您的問題特別要求'SQL語句轉到下一個可用編號'。如果您有關於從函數返回值的單獨問題,則需要創建一個新問題並在那裏提問。我回答了問題。 :-) –

+0

對,謝謝大家的幫助。我設法讓它工作,但是當它按順序進入下一個數字時,它會向前跳兩個。例如,D300029是下一個可用編號,並在窗體中顯示。我關閉它,然後再次提出表單。 D300030代替D300032。我會更多地混淆它。謝謝! –