Execute Non Query Sql Cmd (Signature 2)
Executes a non-query SQL command (such as INSERT, UPDATE, DELETE) on the specified SqlConnection and optionally returns an error message.
This function ensures that the connection is open before executing the command. Unlike the overload that returns an Integer , this version returns a String for error reporting. It does not rethrow exceptions and does not log errors.
VB
Public Function ExecuteNonQuerySqlCmd(MyConnection As SqlConnection,
SqlCmd As String,
ReturnErrorMessage As Boolean) As String
Try
If Not MyConnection.State = ConnectionState.Open Then MyConnection.Open()
Dim MyCmd As New SqlCommand(SqlCmd, MyConnection)
MyCmd.ExecuteNonQuery()
Return String.Empty
Catch ex As Exception
Return ex.Message
End Try
End Function