2011年6月23日星期四

  sqlCommand.ExecuteNonQuery返回sql语句影响行数问题

sqlCommand.ExecuteNonQuery返回sql语句影响行数问题

Public Shared Function sqlcon_Executeint(ByVal sql As String) As Integer '执行sql更新语句并返回影响行数,当为select语句时,请用count(*)样式

Using sqlcmd As New SqlCommand

Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(sql, sqlcon)

Dim rowsAffected As Integer = 0

sqlcon.Open()

Try

If sqlCommand.ExecuteNonQuery <> -1 Then '判断是否是查询语句

rowsAffected = sqlCommand.ExecuteNonQuery '

Else '是查询语句

rowsAffected = sqlCommand.ExecuteScalar

End If

Catch ex As Exception

WriteErr(ex, sql)

Throw New NotSupportedException(ex.ToString)

Finally

sqlcon.Close()

End Try

Return rowsAffected

End Using

End Function

问题:修改,插入语句的返回值正确,删除语句的返回值始终为“0”注:sql返回的为-1(ado.net)

没有评论:

发表评论