从数据库

时间:2016-08-01 14:02:45

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

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

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

为了更清楚地解释这一点,数据库中的每条记录都有自己的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

1 个答案:

答案 0 :(得分:0)

你在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 

当然