sqlProcedure函数:
Dim e As RequestEventArgs = Args(0)
Dim cmd As New SQLCommand
Dim obj As new Jobject ' 返回值,前端根据此进行相应的处理
obj("code") = 0
obj("count") = 0
obj("Info") = ""
' 数据源
Dim strDataSource As String
If e.Values.ContainsKey("dataSource") Then
strDataSource = e.Values("dataSource")
Else
obj("code") = "401"
obj("Info") = "尚未指定dataSource参数!"
End If
' 调用的存储过程
Dim strProcedureName As String
If e.Values.ContainsKey("proName") Then
strProcedureName = e.Values("proName")
Else
obj("code") = "401"
obj("Info") = "尚未指定proName参数!"
End If
Output.show("开始准备参数")
cmd.StoredProcedure = True '表示CommandText内容不是标准的SQL语句,而是存储过程名
' 写入输入参数
Dim bHaveOutPut As Boolean = False ' 是否有输出参数
Dim bHaveReturn As Boolean = False ' 是否有返回值
Dim arrParm As JArray
Dim arrParamName As new List(of String)
If e.Values.ContainsKey("param") Then
arrParm = JArray.parse(e.Values("param"))
Output.show(e.Values("param"))
Dim parName As String = "" ' 存储过程的参数名称
Dim parValue As String = "" ' 存储过程的参数值
Dim parOutput As Boolean = False ' 此参数为返回参数为FALSE, 输出参数为TRUE
Dim bHaveOut As Boolean = False
Dim parSize As Integer = 0 ' 输出参数的字节大小
Dim bHaveSize As Boolean = False
'获取各参数的值
For Each jt As JToken In arrParm
' 数组遍历
' 变量重新初始化
parName = ""
parValue = ""
parOutPut = False
parSize = 0
bHaveOut = False
bHaveSize = False
For Each jp As JProperty In jt
' 集合遍历
Output.Show( jp.name.ToString )
Output.Show(jp.value.ToString)
Select Case jp.name.ToString
Case "name" :
parName = jp.value.ToString
arrParamName.Add(parName)
Case "value" :
parValue = jp.value.ToString
Case "output" :
parOutput = jp.value.ToString
bHaveOut = True
If parOutput = True Then
bHaveOutput = True
Else
bHaveReturn = True
End If
Case "size" :
parSize = Val(jp.value.ToString)
bHaveSize = True
End Select
Next
'根据输入值,选择执行函数
If bHaveOut = True And bHaveSize = True Then ' 输出参数且有size
cmd.Parameters.add( parName , parValue , parOutput , parSize )
ElseIf bHaveOut = True And bHaveSize = False Then ' 输出参数无size
cmd.Parameters.Add( parName , parValue , parOutput)
Else ' 输入参数
cmd.Parameters.Add( parName , parValue)
End If
Next
Output.Show("读取参数")
End If
If obj("code") = "0" Then
If bHaveReturn Or bHaveOutput Then
Output.show("存储过程非查询记录集")
cmd.ExecuteNonQuery
Dim arr1 As New Jarray
For Each ex As String In arrParamName
arr1(0)(ex) = cmd.Parameters(ex)
Next
obj("count") = 1
obj("data") = arr1
Else
Output.show("存储过程查询记录集")
Dim dt As DataTable = cmd.ExecuteReader
Output.show("存储过程查询over")
Output.Show( dt.DataRows.Count)
Output.Show( "111111")
'遍历记录
Dim arr As new JArray
For i As Integer = 0 To dt.DataRows.Count - 1
Output.Show(i)
arr.Add(new JObject)
Dim dr As DataRow = dt.DataRows(i)
For Each dc As DataCol In dt.DataCols
If dc.IsNumeric Then
arr(i)(dc.Caption) = val(dr(dc.Name))
Else
arr(i)(dc.Caption) = dr(dc.Name).ToString()
End If
Next
Next
'生成json
obj("count") = val(dt.DataRows.Count)
obj("data") = arr
End If
End If
'返回到页面
e.WriteString(CompressJson(obj))
e.Handled = True