从jQuery将连接对象传递给ASP.Net webservice

时间:2013-03-27 08:44:24

标签: jquery asp.net web-services

我有一个提供AS400数据的网络服务。 这个web服务有一个固定的连接字符串,我希望它是动态的。参见下面的代码:

    Public Function GetData(sql As String) As DataTable

            Dim _data As DataTable

            Try

                Dim s As String = "Provider=IBMDA400"

                s &= ";Data Source=" & "x.x.x.x"
                s &= ";User Id=" & "USER"
                s &= ";Password=" & "PASS"
                s &= ";Default Collection=" & "AAABBB"

                con = New OleDbConnection(s)

                Dim cmd As New OleDbCommand

                With cmd
                    .Connection = con
                    .CommandType = CommandType.Text
                    .CommandText = sql
                End With

                Dim dap As New OleDbDataAdapter(cmd)

                con.Open()

                Dim dat As New DataSet

                dap.Fill(dat)

                con.Close()
                con.Dispose()

                _data = dat.Tables(0)

            Catch ex As Exception

                _data = New DataTable

            End Try

            Return _data

        End Function

使用上述内容的示例Web服务函数:

<WebMethod()> _
        Public Function GetPicklistLines(picklist As String) As List(Of PicklistLine)

            Dim lines As New List(Of PicklistLine)
            Dim line As PicklistLine

            sql = "select " & _
                                    "OLPLNO,OLPLLI,OLORNO,OLLINE,OLPRDC,OLDESC,OLOQTS,OLCQTS,OLUNIT,OLORDS,PGDRNR, AAV01, AAV02, NAV03 " & _
                                    "from SRBSOL " & _
                                    "left outer join SRBPRG on PGPRDC=OLPRDC " & _
                                    "left outer join Z2OOCFGF on F0ERNC=OLORNO and F0A2NB=OLLINE " & _
                                    "where OLPLNO=" & picklist & " and OLSTAT <>' D'"

            data = GetData(sql)

            ' TODO: insert ROW 0 to set active:
            sql = "insert " & _
                    "into SRBSOL " & _
                    "(OLPLNO, OLPLLI) " & _
                    "values " & _
                    "(" & picklist & "," & 0 & ") " & _
                    "where OLPLNO=" & picklist & " and OLSTAT <>' D'"

            For Each dr As DataRow In data.Rows

                ' TODO: get reported quantity:
                sql = "select " & _
                        "RCBAQT " & _
                        "from Z2OOREXC " & _
                        "where RCPLNO='" & picklist & "' and RCPLLI='" & dr("OLPLLI") & "'"

                line = New PicklistLine

                With line
                    .PicklistNumber = picklist
                    .OrderNumber = IIf(TypeOf (dr("OLORNO")) Is DBNull, "", dr("OLORNO"))
                    .PicklistLinenumber = IIf(TypeOf (dr("OLPLLI")) Is DBNull, "", dr("OLPLLI"))
                    .Item = IIf(TypeOf (dr("OLPRDC")) Is DBNull, "", dr("OLPRDC"))
                    .ItemDescription = IIf(TypeOf (dr("OLDESC")) Is DBNull, "", dr("OLDESC"))
                    .InnerColor = IIf(TypeOf (dr("AAV01")) Is DBNull, "", dr("AAV01"))
                    .OuterColor = IIf(TypeOf (dr("AAV02")) Is DBNull, "", dr("AAV02"))
                    .Length = IIf(TypeOf (dr("NAV03")) Is DBNull, "", dr("NAV03"))
                    .Unit = IIf(TypeOf (dr("OLUNIT")) Is DBNull, "", dr("OLUNIT"))
                    .Quantity = IIf(TypeOf (dr("OLCQTS")) Is DBNull, "", CInt(dr("OLCQTS")))
                    .Needed = IIf(TypeOf (dr("OLOQTS")) Is DBNull, "", CInt(dr("OLOQTS")))
                End With

                lines.Add(line)

            Next

            Return lines

        End Function

上面的函数由jquery调用:

function GetAllPicklists() {
    $('#page_overview_search').addClass('ui-disabled');
    $("#Picklists").html('');
    //$("#MemberList").addClass("loading");
    $.ajax({
        type: "POST",
        url: "http://" + host + "/services/picklists.asmx/GetAllPicklists",
        data: "{'customer':'" + $('#select-customer').val() + "', 'route':'" + $('#select-route').val() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnGetAllPicklistsSuccess,
        error: OnError
    });
}

我已经修改了webservice函数以提供JSON,但这项工作正在进行中......:)

一切都按预期工作,但我希望连接字符串 s 来自 GetData 动态。我应该如何设置一个(尚未定义的)连接对象,其中包含数据源(IP),用户ID 密码和<的值strong>默认收藏。 具体如何在jquery POST data 参数中传递该连接对象和服务函数调用参数?是否可以在将来登录时设置此连接对象?

请随时提出问题以澄清您不清楚的事项。

提前致谢!

1 个答案:

答案 0 :(得分:0)

通过服务调用传递数据不是一个好的选择。你的sql连接字符串很容易被黑客入侵。您可以使用wcf服务并使用消息合同来传递信息。