【问题标题】:Return maximum ID number from a database从数据库返回最大 ID 号
【发布时间】:2016-08-01 14:02:44
【问题描述】:

这可能是一件容易的事,但我只是在做我的第一个 vb 项目,所以我不确定如何 100% 做到这一点,所以如果以下问题实际上非常简单,请提前道歉。

基本上,我需要做的是,在将数据库表加载到超网格时,我需要检索存储在字段中的最大整数。

为了更清楚地解释这一点,数据库中的每条记录都有自己的 ID 号,所以我需要迭代每条记录,找到 ID 号最高的记录,并返回 ID,这样就可以在其他计算。

我知道我可以使用SQL = SELECT MAX(supportID) FROM tblIncidents 来检索存储在此表中的最高 ID 号。

那么,我该如何将这个结果(即最高 ID 号)声明为变量,以便我可以首先将其显示在消息框中以向我证明查询有效,其次以便我可以在整个代码中使用该变量作为使用 ID 的方法吗?

一个例子;这是将新记录保存到 tblIncidents 表中的代码。

 Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

    Dim incidentSolved As Boolean = False
    If cboxSolved.Checked Then
        incidentSolved = True
    End If

    If txtClientSave.Text = "" Then
        MsgBox("Client name cannot be blank")

    ElseIf rtbProblem.Text = "" Then
        MsgBox("Problem cannot be blank")

    ElseIf cboxSolved.Checked = True And rtbSolution.Text = "" Then
        MsgBox("Please enter solution")

    Else
        database.SaveNewIncident(txtClientSave.Text, dtpStart.Value, dtpEnd.Value, rtbProblem.Text, dtpStartTime.Value, dtpEndTime.Value, cboxSolved.Checked, rtbSolution.Text, _con)

        txtClientSave.Text = ""
        rtbProblem.Text = ""
        rtbSolution.Text = ""
        dtpStart.Value = Date.Today
        dtpEnd.Value = Date.Today
        dtpStartTime.Value = DateTime.Now
        dtpEndTime.Value = DateTime.Now
        cboxSolved.Checked = False

    End If
End Sub

被调用的数据库函数

Public Shared Function SaveNewIncident(ByVal clientName As String, dateStart As Date, dateEnd As Date, ByVal incidentProblem As String, ByVal timeStart As String, ByVal timeEnd As String,
                                       ByVal incidentSolved As Boolean, ByVal incidentSolution As String, _Con As OleDbConnection)

    Dim tr As OleDbTransaction = Nothing

    Try
        tr = _Con.BeginTransaction()

        Dim Dc As New OleDbCommand
        Dc.Connection = _Con

        Dc.CommandType = CommandType.Text
        Dc.CommandText = "INSERT INTO dbo.tblIncidents VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"
        Dc.Transaction = tr
        Dc.Parameters.Add("@clientName", OleDbType.VarChar).Value = clientName
        Dc.Parameters.Add("@dateStart", OleDbType.Date).Value = dateStart
        Dc.Parameters.Add("@dateEnd", OleDbType.Date).Value = dateEnd
        Dc.Parameters.Add("@incidentProblem", OleDbType.LongVarChar).Value = incidentProblem
        Dc.Parameters.Add("@timeStart", OleDbType.VarChar).Value = timeStart
        Dc.Parameters.Add("@timeEnd", OleDbType.VarChar).Value = timeEnd
        Dc.Parameters.Add("@incidentSolved", OleDbType.Boolean).Value = incidentSolved
        Dc.Parameters.Add("@incidentSolution", OleDbType.LongVarChar).Value = incidentSolution

        Dc.ExecuteNonQuery()

        tr.Commit()

        MsgBox("Save successful")

    Catch ex As Exception

        mdInit.errorLog(ex.Message, ex.StackTrace)
        MsgBox("Failed to save data, refer to error log")
        tr.Rollback()

    End Try

End Function

【问题讨论】:

  • 到目前为止你尝试过什么?此外,这种类型的逻辑通常不是一个好方法,因为如果您检索到该值后,其他人插入了一个新值会发生什么?
  • @SeanLange 嗨,肖恩,这个应用程序仅供 1 人使用,因此不会发生插入混淆。我需要这样做的原因是因为我在程序中编写了一个导入功能来用新数据库覆盖现有数据库,但这意味着在 SQL Server 中关闭 Identity_Insert。所以现在在我的保存按钮上,我需要检索最高的当前 ID 并增加它以确定进入 ID 列的值
  • 为什么不插入行并使用 OUTPUT 子句?这样你就不必希望你的价值是一样的。
  • @SeanLange 至于我所尝试的,我现在只想返回 MsgBox 中的最高 ID,只是为了显示问题中的代码有效。我将 sql 声明为 OleDbCommand 并且在下面我包含的代码我做了MsgBox(sql) 但是这不起作用,因为它说对象引用未设置为对象的实例
  • 我们可能会帮助您修复现有问题,但您实际上还没有发布任何代码。

标签: sql sql-server database vb.net iteration


【解决方案1】:

您可以在 VB.net 中定义一个 SQL 命令,该命令需要附加一个 SQL 连接 - 因为您的查询返回单个值,您将能够使用 ExecuteScalar 执行它以返回单个值,例如

Dim cmd AS New SqlClient.SQLCommand("SELECT MAX(billID) FROM tblRecurring",connectionString) 

然后

cmd.connection.open 

然后

dim x as MaxId = cmd.executeScalar 

并以

结束
cmd.connection.dispose 

当然

【讨论】:

  • 嗨 Andrew,'Dim x As MaxID` 行不起作用,因为 maxID 没有被声明?
猜你喜欢
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 2018-06-24
  • 1970-01-01
  • 2018-03-31
  • 2018-09-08
  • 1970-01-01
相关资源
最近更新 更多