运行时错误' 91'使用MSSql时

时间:2016-02-09 05:25:47

标签: sql-server vb6

我正在编写一个程序,在尝试将值保存在不同的表中之前,我必须检查另一个表中的某些列值。这两个表都在SQL中。

我尽我所能自己做,但我在突出显示的行附近得到了错误。

rs.open(insert into testreport_tb1...

Private Sub Command1_Click()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim BrdSrNo As String
Dim Result As Boolean
Dim machineName As String

machineName = Environ("computername")
' Ready objects for use.
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
BrdSrNo = BoardSrNo.Text
Result = False
' Connect.
cn.Open "{Here I give the connection string}"
' Fetch a recordset.
rs.Open "select * from testreport_tb1 where board_SrNo = '" & BrdSrNo & "' order by test_DateTime desc", cn, adOpenStatic, adLockReadOnly
' Display value, and total recordcount.
MsgBox rs.Fields(3)
MsgBox rs.Fields(8)
'MsgBox rs.RecordCount
stage_Status = rs.Fields(3)
stage_Id = rs.Fields(8)
rs.Close
cn.Close

If stage_Status = "C" Then
If stage_Id = "True" Then
rs.Open "insert into testreport_tb1 values('" & BrdSrNo & "',3,GETDATE(),'" & Result & "',NULL,'" & machineName & "',' KO ','A','D')", cn, adOpenDynamic, adLockBatchOptimistic
MsgBox "saved"
End If
End If
' Close and release objects.
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing

End Sub

1 个答案:

答案 0 :(得分:2)

据我记忆,在执行DML statements,(插入,更新或删除)时不能使用rs.Open,但只有在执行select语句时才能使用If stage_Status = "C" And stage_Id = "True" Then Dim cmd as new ADODB.Command cmd.CommandText = "insert into testreport_tb1 values(?, 3, GETDATE(), ?, NULL, ?, ' KO ', 'A', 'D')" cmd.ActiveConnection = cn Set param = cmd.CreateParameter(, adVarChar, adParamInput) param.Value = BrdSrNo cmd.Parameters.Append param Set param = cmd.CreateParameter(, adVarChar, adParamInput) param.Value = Result cmd.Parameters.Append param Set param = cmd.CreateParameter(, adVarChar, adParamInput) param.Value = machineName cmd.Parameters.Append param cmd.Execute MsgBox "saved" End If

此外,您需要使用ADODB.Command并设置参数而不是连接字符串来创建插入语句,否则它会成为sql injection攻击的开门。

自从我上次使用ADODB以来已经很长时间了,但是你的插入代码应该是这样的:

angular.module('Y', [
  'ngRoute',
  'mobile-angular-ui',
  'Y.controllers.Main',
   'ngSanitize'
])
.config(function($routeProvider) {
  $routeProvider.when('/', {templateUrl:'home.html',  reloadOnSearch: null})
                .when('/cityPage', {templateUrl:'cityPage.html', reloadOnSearch: false})
                .when('/category-prduct', {templateUrl:'category-prduct.html',  reloadOnSearch: false})
                .when('/product-description', {templateUrl:'product-description.html',  reloadOnSearch: false})
                .when('/my-winni', {templateUrl:'my-winni.html',  reloadOnSearch: false})
                .when('/gift-box', {templateUrl:'gift-box.html',  reloadOnSearch: false});
});

注意:代码直接写在这里,正如我所写的那样,自从我使用ADODB以来已经有很长一段时间了,所以代码中可能存在错误。但是,这是使用ADODB执行insert语句的正确方法。