2013-02-09 82 views
0

我有一個SELECT語句ListViewListBox甚至DataGrid唯一的問題是,我不能顯示的結果我TextBox我只想用:SQL Server的結果

的MAX()函數

我想用MAX(),因爲它說,它會返回選定列的值拉爾,因爲我用INCREMENT我的ID MAX()˚F聯合是我做到這一點的一種方式。

我用這個代碼生成ID:

Dim p1num As Integer = 0 
     p1num += 1 
txtPNumber.Text = p1num.ToString("D4") 
+0

你的代碼在哪裏?........你的問題在哪裏? – andy 2013-02-09 11:48:05

回答

1

我試圖理解你的問題,我認爲你應該使用這樣的:

嘗試:

  Dim querystring As String = "SELECT MAX(pIDNo) FROM (Name of your Table)" 
      Using connection As New SqlConnection("Data Source=(local);Initial Catalog=(Name of your DB);Persist Security Info=True;User ID=(Your User);Password=(Your Pass)") 
       Dim command As New SqlCommand(querystring, connection) 
       connection.Open() 
       Dim reader As SqlDataReader = command.ExecuteReader 
       Dim value = String.Empty 
       While reader.Read 
        value = reader.GetString(0) 
       End While 
       txtPNumber.Text = Today.Year.ToString().Substring(2, 2) & Today.Month.ToString().PadLeft(2, "0") & (Integer.Parse(value.Substring(4, value.Length - 4)) + 1).ToString("D4") 
      End Using 
     Catch ex As Exception 
      txtPNumber.Text = Today.Year.ToString().Substring(2, 2) & Today.Month.ToString().PadLeft(2, "0") & num.ToString("D4") 
     End Try 

儘量使一個Private Sub與它一起使用,如果你想在程序運行之後顯示它,或者如果你想要一個觸發器來顯示你想要的ID,你可以在按鈕上使用它。