说明:conn和sql_data为模块变量
问题:vb6.0中如何调用带参数的存储过程?存储过程本人测试过执行结果没问题!但是按照如下代码出现语法错误或者违反访问规则提示!代码如下:菜鸟请大师们指点。
Dim mycommand As ADODB.Command '命令
Dim canshu As ADODB.Parameter '参数1
Dim connc As String
connc = App.Path
If Right(connc, 1) <> "\" Then
connc = connc + "\"
End If
connc = "Driver={sql server};server=10.103.77.29;uid=sa;pwd=123321;database=kaoshitixong"conn.Open conncSet canshu = New ADODB.Parameter
Set mycommand = New ADODB.Command
' canshu.Name = "name1"
canshu.Type = adVarChar '参数类型
canshu.Size = 50 '参数长度
canshu.Direction = adParamInput
'参数方向,输入或输出
canshu.Value = text1.text  '参数的值
mycommand.Parameters.Append canshu '加入参数mycommand.ActiveConnection = conn
mycommand.CommandText = gerenchengji
mycommand.CommandType = adCmdStoredProc
Set sql_data = New ADODB.Recordset
Set sql_data = mycommand.Execute(-1, canshu, adCmdStoredProc)
Set MSHFlexGrid1.DataSource = sql_data '将记录集赋值给MSHFlexGrid1
Set sql_data = Nothing
conn.Close
  

解决方案 »

  1.   

    http://www.chinaue.com/tool/ado/mdmthcmdexecute.htm
      

  2.   

    参数RecordsAffected   可选,长整型变量,提供者向其返回操作所影响的记录数目
      

  3.   

    但是我尝试了不加任何参数同样报错
    Set sql_data = mycommand.Execute()
    请高人指点谢谢!!
      

  4.   

    mycommand.CommandText = gerenchengji
     gerenchengji 是变量吗?
    取出其值,用企业管理器、VB的数据库访问界面等,执行一下,看看结果。
    还有,测试连接是否可用。
    没有其它情况了~~
      

  5.   

    gerenchengji是存储过程名 在查询分析中执行如下
    execute gerenchengji '112002-4'
    得到的结果,证明过程是没有错的啊,不知道问题在哪里。连接没有问题。我的所有连接都吃这样操作的,调用没有参数的存储过程的连接也是这样的,结果正常。有参数的这个就不正常了。本人业余爱好,请大侠帮忙谢谢!!
    112002-4 112002     109 A         
    112002-4 112002     108 C         
      

  6.   

    写法1:
    Set sql_data = mycommand.Execute("gerenchengji('" & text1.text& "')", , adCmdStoredProc)
    写法2:
     ...
      

  7.   


    Set sql_data = conn.Execute("gerenchengji('" &amp; text1.text&amp; "')", , adCmdStoredProc)
      

  8.   

    '2
    With mycommand
      .CommandType = adCmdStoredProc
      .CommandText = "gerenchengji"
      .Prepared = True
      .Parameters.Append .CreateParameter("@Value", adChar, adParamInput, 20, text1.text)
       Set sql_data  = .Execute
    End With'3
     with mycommand
          .CommandType = adCmdStoredProc
          .CommandText =  "gerenchengji"
          .Parameters(1).Value = text1.text
          Set sql_data  = .Execute
    end with
      

  9.   

    mycommand.CommandText = "gerenchengji"模块最前面把option explicit 安全检查加上
      

  10.   

    With mycommand
      .CommandType = adCmdStoredProc
      .CommandText = "gerenchengji"
      .Prepared = True
      .Parameters.Append .CreateParameter("@Value", adChar, adParamInput, 20, text1.text)
      Set sql_data = .Execute
    End With