簡體   English   中英

獲取受影響的行數

[英]Getting affected row count

更新 1:

這看起來對嗎?

Dim objSqlConnection As SqlConnection
Dim objSqlCommand As SqlCommand

Dim intAffectedRowCount As Integer

Function update_function() As Integer
    Using objSqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connectionString"))
        objSqlCommand = New SqlCommand("update table1 set col1 = @col1 where id = @id", objSqlConnection)

        objSqlCommand.Parameters.Add("@col1", SqlDbType.VarChar, 255).Value = strData
        objSqlCommand.Parameters.Add("@id", SqlDbType.Int, 4).Value = intID

        objSqlCommand.Connection.Open()
        intAffectedRowCount = objSqlCommand.ExecuteNonQuery()
        objSqlCommand.Connection.Close()
    End Using

    return intAffectedRowCount
End Function

這會關閉所有正確打開的東西嗎?


原始問題:

我目前正在使用以下代碼來更新數據庫表中的一行:

objSQLConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString"))

objSQLCommand = New SqlCommand("update table1 set col1 = @col1 where id = @id", objSQLConnection)

objSQLCommand.Parameters.Add("@col1", SqlDbType.VarChar, 255).Value = strCol1Data
objSQLCommand.Parameters.Add("@id", SqlDbType.Int, 4).Value = intID

objSQLCommand.Connection.Open()
objSQLCommand.ExecuteNonQuery()
objSQLCommand.Connection.Close()

如果我在 sql 服務器中運行該查詢,它會返回一條消息說“1 行受影響”。

可能可以從 asp.net 訪問相同的消息,但我不知道如何訪問它。

即使我得到一個計數,即1。

有人知道如何計算受影響的行數嗎?

SqlCommand.ExecuteNonQuery的文檔中:

返回值

類型:System.Int32 受影響的行數。

因此,在您執行ExecuteNonQuery並且當前忽略返回值的地方,請不要忽略它:)

(您還應該使用Using語句來確保您不會泄漏連接等)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM