ADO用Execute执行插入,删除,修改操作,怎么判断有没有成功
如题:
除了判断执行是否成功,还有如何拿到更改的记录条数。
------解决方案--------------------1.
用事务去判断是否执行成功?如果执行不成功会有错误返回。
也可以有try ..... catch
2.
insert into tb(....) values(....)
select @@ROWCOUNT -->加这一行
@@ROWCOUNT是系统变量,返回所影响的行数
------解决方案--------------------默认的话,成功返回0;失败返回相关错误码(小于0).
楼主可在存储过程里,强制返回UPDATE记录数。
declare @updatecount int
update tb set fieldvalue = 2 where id = 1
set @updatecount = @@rowcount
return @updatecount
参考:
http://www.cnblogs.com/net515/archive/2012/06/10/2544131.html
------解决方案--------------------
''' <summary>
''' 使用OleDbConnection执行Sql
''' </summary>
''' <param name="sql"></param>
''' <param name="myConnection"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function ExecSql(ByVal sql As String, ByVal myConnection As OleDb.OleDbConnection) As Long
Dim myTrans As OleDb.OleDbTransaction
Try
Dim myCommand As OleDb.OleDbCommand
myCommand = New OleDb.OleDbCommand()
If myConnection.State = ConnectionState.Closed Then
Try
myConnection.Open()