如何将参数传递给SQLDataSource [英] How Do I pass Parameters to a SQLDataSource

查看:72
本文介绍了如何将参数传递给SQLDataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经在这个问题上工作了半天,似乎无法弄清楚.

我在表单中设置了一个sqldatasource,带有2个参数以过滤记录.
我需要将参数传递给sqldatasource,以查看数据是否已经存在.

我不知道是否没有点击或我只是不了解它是如何工作的.
我是否要使用数据层,但是被告知这是一个原型,我只需要完成它即可.

任何帮助,将不胜感激.


sqldatasource

Okay, I''ve been working on this issue for half a day and can''t seem to figure it out.

I have a sqldatasource set up in my form with 2 parameters to filter the records.
I need to pass the parameters to the sqldatasource to see if the data already exist.

I don''t know if something isn''t clicking or if I''m just not understanding how this works.
I would whether use a data tier, but have been told this is a prototype and I just need to complete it.

Any help would be appreciated.


sqldatasource

<asp:SqlDataSource ID="dsAddPermission" runat="server"

                    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

                    InsertCommand="proc_AddPermission"                      InsertCommandType="StoredProcedure"

                    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"

                    SelectCommand="SELECT [ID], [AccountID] FROM [Permission] WHERE (([ID] = ?) AND ([AccountID] = ?))">
  <InsertParameters>
    <asp:Parameter Name="AccountID" Type="Int32" />
    <asp:Parameter Name="EmployeeID" Type="Int32" />
  </InsertParameters>
  <SelectParameters>
    <asp:Parameter Name="MyRadID" Type="Int32" />
    <asp:Parameter Name="AccountID" Type="Int32" />
  </SelectParameters>
</asp:SqlDataSource>




检索数据的代码




code to retrieve data

Private Function CheckPermissions(ByVal aID As Integer, ByVal eID As Integer) As Boolean
  Dim bCheck As Boolean
  Dim ds = New DataSet()

  ds = dsAddPermission.Select(DataSourceSelectArguments.Empty)

  If IsDBNull(ds.Tables) Then
    bCheck = False
  ElseIf ds.Tables.Count > 0 Then
    bCheck = True
  Else
    bCheck = False
  End If

  Return bCheck
End Function



[已修改:代码中的格式化选项卡]



[Modified: formatted tabs in code]

推荐答案

ConnectionStrings:ConnectionString %> " InsertCommand =" InsertCommandType =" 存储过程" ProviderName =" <%
ConnectionStrings:ConnectionString %>" InsertCommand="proc_AddPermission" InsertCommandType="StoredProcedure" ProviderName="<%


ConnectionStrings:ConnectionString.ProviderName SelectCommand =" < InsertParameters > < asp:Parameter 名称 =" 类型 Int32 " / < asp:Parameter 名称 =" 类型 Int32 " / < /InsertParameters > < SelectParameters > < asp:Parameter 名称 =" 类型 Int32 " / < asp:Parameter 名称 =" 类型 Int32 " / < /SelectParameters > < /asp:SqlDataSource >
ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT [ID], [AccountID] FROM [Permission] WHERE (([ID] = ?) AND ([AccountID] = ?))"> <InsertParameters> <asp:Parameter Name="AccountID" Type="Int32" /> <asp:Parameter Name="EmployeeID" Type="Int32" /> </InsertParameters> <SelectParameters> <asp:Parameter Name="MyRadID" Type="Int32" /> <asp:Parameter Name="AccountID" Type="Int32" /> </SelectParameters> </asp:SqlDataSource>




检索数据的代码




code to retrieve data

Private Function CheckPermissions(ByVal aID As Integer, ByVal eID As Integer) As Boolean
  Dim bCheck As Boolean
  Dim ds = New DataSet()

  ds = dsAddPermission.Select(DataSourceSelectArguments.Empty)

  If IsDBNull(ds.Tables) Then
    bCheck = False
  ElseIf ds.Tables.Count > 0 Then
    bCheck = True
  Else
    bCheck = False
  End If

  Return bCheck
End Function



[修改:代码中的格式化标签]



[Modified: formatted tabs in code]


好.我和老板坐下来,我们想出了这个办法.我需要将参数添加到数据源,然后绑定数据.完成后,我便可以将信息放入视图中,并检查是否返回了值.

我们没有在sqldatasource端更改任何功能,仅在函数上更改了.

以下是我们使用的解决方案.

Okay. I sat do with my boss and we figured this out. I needed to add the parameters to the datasource and then bind the data. Once I did that I was able to put the information into a view and check if a value had been returned.

We didn''t change anything on the sqldatasource end, just on the function.

The following is the solution we used.

Private Function CheckPermissions(ByVal aID As Integer, ByVal eID As Integer) As Boolean

        Dim bCheck As Boolean
        Dim dv = New DataView()

        dsAddPermission.SelectParameters("AccountID").DefaultValue = aID.ToString
        dsAddPermission.SelectParameters("MyRadID").DefaultValue = eID.ToString
        dsAddPermission.DataBind()

        dv = dsAddPermission.Select(DataSourceSelectArguments.Empty)

        If IsDBNull(dv.Table) Then
            bCheck = False
        ElseIf dv.Table.Rows.Count > 0 Then
            bCheck = True
        Else
            bCheck = False
        End If

        Return bCheck

    End Function


这篇关于如何将参数传递给SQLDataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆