VB中调用存储过程,参数赋值求助??

user 2011-05-17 08:47:44
存储过程中,参数是这样的:
DECLARE @n_jc1 tinyint
DECLARE @n_jc2 tinyint
DECLARE @kmlxs nvarchar(500)

SELECT @n_jc1 = 1
SELECT @n_jc2 = 3
SELECT @kmlxs = N'case when cclass =N''资产'' then 1 else case when cclass =N''负债'' then 2 else case when cclass =N''权益'' then 3 else case when cclass =N''成本'' then 4 else 5 end end end end as lx'

EXEC @RC = [UFDATA_100_2011].[dbo].[GL_P_FSEYEB] @iPer1, @iPer2, @ibk, @c_code1, @c_code2, @n_jc1, @n_jc2, @isend, @kmlx, @exch, @yefw1, @yefw2, @kmlxs, @TRnd, @skmZdyType

在VB中,为参数赋值:
g_Cmd.Parameters(1) = 1
g_Cmd.Parameters(2) = 3

g_Cmd.Parameters(3)=? ‘第三个参数,我应该如何赋值?如果按照上面的写法会报错。
...全文
142 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
user 2011-05-18
  • 打赏
  • 举报
回复
谢谢各位,问题已解决。
user 2011-05-17
  • 打赏
  • 举报
回复
我的源程序是这样的:
Private Sub commandbutton1_Click()
Dim i As Integer, sht As Worksheet 'i为整数变量;sht 为excel工作表对象变量,指向某一工作表
Dim cn As New adodb.Connection '定义数据链接对象 ,保存连接数据库信息;请先添加ADO引用
Dim rs As New adodb.Recordset '定义记录集对象,保存数据表
Dim strCn As String, strSQL As String '字符串变量
Set sht = ThisWorkbook.Worksheets("sheet1")
strCn= "Provider=sqloledb;Server=ipsundb;Database=ufdata_100_2011"+ ";Uid=sa;Pwd=abc123;"

cn.Open strCn '与数据库建立连接,如果成功,返回连接对象cn


Set g_Cmd = New Command

g_Cmd.ActiveConnection = strCn ' 连接到数据库
g_Cmd.CommandType = adCmdStoredProc ' 表示cmd的类型为存储过程
g_Cmd.CommandText = "GL_P_FSEYEB" ' 调用存储过程

g_Cmd.Parameters(1) = "1"
g_Cmd.Parameters(2) = "5"
g_Cmd.Parameters(3) = 0
g_Cmd.Parameters(4) = Null
g_Cmd.Parameters(5) = "我"
g_Cmd.Parameters(6) = 1
g_Cmd.Parameters(7) = 3
g_Cmd.Parameters(8) = 0
g_Cmd.Parameters(9) = Null
g_Cmd.Parameters(10) = Null
g_Cmd.Parameters(11) = Null
g_Cmd.Parameters(12) = Null
g_Cmd.Parameters(13) = "case when cclass ='资产' then 1 else case when cclass ='负债' then 2 else case when cclass ='权益' then 3 else case when cclass ='成本' then 4 else 5 end end end end as lx"
g_Cmd.Parameters(14) = "YEB26753"
g_Cmd.Parameters(15) = Null

Set g_Rs = g_Cmd.Execute ' 执行存储过程 把数据库查询结果返回给数据集

rs.Open g_Cmd.Execute, cn '执行strSQL所含的SQL命令,结果保存在rs记录集对象中
i = 5

Do While Not rs.EOF '当数据指针未移到记录集末尾时,循环下列操作
sht.Cells(i, 1) = rs(1) '把当前记录的字段1的值保存到sheet1工作表的第i行第1列
sht.Cells(i, 2) = rs(2)
sht.Cells(i, 3) = rs(3)
sht.Cells(i, 4) = rs(4)
sht.Cells(i, 5) = rs(5)
sht.Cells(i, 6) = rs(6)
sht.Cells(i, 7) = rs(7)
sht.Cells(i, 8) = rs(8)
sht.Cells(i, 9) = rs(9)

rs.MoveNext '把指针移向下一条记录
i = i + 1 'i加1,准备把下一记录相关字段的值保存到工作表的下一行
Loop '循环
rs.Close
cn.Close '关闭数据库链接,释放资源
MsgBox "查询完毕!"
End Sub
user 2011-05-17
  • 打赏
  • 举报
回复
是这样写吗?
g_Cmd.Parameters(13) = "case when cclass ='资产' then 1 else case when cclass ='负债' then 2 else case when cclass ='权益' then 3 else case when cclass ='成本' then 4 else 5 end end end end as lx"
ForFumm 2011-05-17
  • 打赏
  • 举报
回复
第三个参数用字符串拼接的SQL语句
AcHerat 元老 2011-05-17
  • 打赏
  • 举报
回复
VB里怎么拼字符串的呢拼进去就是了,只要打印出来和SQL的一样就行。
dearbinge 2011-05-17
  • 打赏
  • 举报
回复
设定参数类型行不行?
user 2011-05-17
  • 打赏
  • 举报
回复
整个存储过程:
DECLARE @RC int
DECLARE @iPer1 nvarchar(10)
DECLARE @iPer2 nvarchar(10)
DECLARE @ibk tinyint
DECLARE @c_code1 nvarchar(60)
DECLARE @c_code2 nvarchar(60)
DECLARE @n_jc1 tinyint
DECLARE @n_jc2 tinyint
DECLARE @isend tinyint
DECLARE @kmlx nvarchar(20)
DECLARE @exch nvarchar(20)
DECLARE @yefw1 nvarchar(20)
DECLARE @yefw2 nvarchar(20)
DECLARE @kmlxs nvarchar(500)
DECLARE @TRnd nvarchar(20)
DECLARE @skmZdyType nvarchar(100)
SELECT @iPer1 = N'1'
SELECT @iPer2 = N'5'
SELECT @ibk = 0
SELECT @c_code1 = NULL
SELECT @c_code2 = N'我'
SELECT @n_jc1 = 1
SELECT @n_jc2 = 3
SELECT @isend = 0
SELECT @kmlx = NULL
SELECT @exch = NULL
SELECT @yefw1 = NULL
SELECT @yefw2 = NULL
SELECT @kmlxs = N'case when cclass =N''资产'' then 1 else case when cclass =N''负债'' then 2 else case when cclass =N''权益'' then 3 else case when cclass =N''成本'' then 4 else 5 end end end end as lx'
SELECT @TRnd = N'YEB26753'
SELECT @skmZdyType = NULL
EXEC @RC = [UFDATA_100_2011].[dbo].[GL_P_FSEYEB] @iPer1, @iPer2, @ibk, @c_code1, @c_code2, @n_jc1, @n_jc2, @isend, @kmlx, @exch, @yefw1, @yefw2, @kmlxs, @TRnd, @skmZdyType


在VB中,为参数赋值:
g_Cmd.Parameters(1) = "1"
g_Cmd.Parameters(2) = "5"
g_Cmd.Parameters(3) = 0
g_Cmd.Parameters(4) = Null
g_Cmd.Parameters(5) = "我"
g_Cmd.Parameters(6) = 1
g_Cmd.Parameters(7) = 3
g_Cmd.Parameters(8) = 0
g_Cmd.Parameters(9) = Null
g_Cmd.Parameters(10) = Null
g_Cmd.Parameters(11) = Null
g_Cmd.Parameters(12) = Null
g_Cmd.Parameters(13) = ????????????’这个应该如何赋值???
g_Cmd.Parameters(14) = "YEB26753"
g_Cmd.Parameters(15) = Null

第13个参数应该如何赋值???
--小F-- 2011-05-17
  • 打赏
  • 举报
回复
建议去VB版问问
user 2011-05-17
  • 打赏
  • 举报
回复
请高手帮忙解答!致谢!

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧