【问题标题】:Using Output Parameter in SqlDataSource在 SqlDataSource 中使用输出参数
【发布时间】:2011-11-15 16:42:42
【问题描述】:

我正在检索 db 条目的 scope_identity,并且我想在不同的 SqlDataSource 中使用该变量,主要用作但我无法访问该变量。 我将变量显示在 msgbox 中,并且显示正确,我只是不确定如何在 SqlDataSource 中访问它。这是我的代码;

这是插入第一个信息并接收到scope_identity,以及_inserted事件的数据源; 代码:

<asp:SqlDataSource ID="InsertPatientInfo" runat="server" ConnectionString="<%$ ConnectionStrings:DataConnectionString %>"
providername="<%$ ConnectionStrings:DataConnectionString.ProviderName %>"
    InsertCommandType="StoredProcedure"
    InsertCommand = "InsertPatInfo"
    OnInserted="InsertPatientInfo_Inserted">
    <InsertParameters>
        <asp:ControlParameter ControlID = "PatInfoName" Name="PatName" PropertyName="text"/>
        <asp:ControlParameter ControlID = "PatInfoAge" Name="PatAge" PropertyName="text" />
        <asp:ControlParameter ControlID = "PatInfoState" Name="PatState" PropertyName="selectedvalue" />
        <asp:ControlParameter ControlID = "PatInfoCountry" Name="PatCountry" PropertyName="selectedvalue" />
        <asp:ControlParameter ControlID = "PatInfoPhone" Name="PatPhone" PropertyName = "text" />
        <asp:ControlParameter ControlID = "PatInfoCell" Name="PatCell" PropertyName="Text" />
        <asp:Parameter DbType="Int32" Direction="Output" Name="PatID" />
    </InsertParameters> 
Protected Sub InsertPatientInfo_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles InsertPatientInfo.Inserted
    Dim PatID As String = e.Command.Parameters("@PatID").Value
    MsgBox(PatID, MsgBoxStyle.Critical)
End Sub

这是对下一个 SqlDataSource 的调用,包括 InsertCommand,我计划在它工作后切换到存储过程;我继续收到错误,标量变量@PatID 未设置,我应该将其声明为 a 吗?如果是,是什么类型? 代码:

      <asp:SqlDataSource ID="InsInqInfo" runat="server" ConnectionString="<%$ ConnectionStrings:DataConnectionString %>"
        providerName="<%$ ConnectionStrings:DataConnectionString.ProviderName %>"
        InsertCommand = "Insert into tblInquirer(InqPatID, InqName, InqState, InqCountry, InqPhone, InqRelation, InqVia, InqCareLevel, InqProgram) VALUES 
        (@PatID, @InqName, @InqState, @InqCountry, @InqPhone, @InqRelation, @InqVia, @InqCareLevel, @InqProgram)"


<InsertParameters>

            <asp:ControlParameter ControlID = "InqName" Name="InqName" PropertyName="text"/>
            <asp:ControlParameter ControlID = "InqStateList" Name="InqState" PropertyName="selectedvalue" />
            <asp:ControlParameter ControlID = "InqCountry" Name="InqCountry" PropertyName="selectedvalue" />
            <asp:ControlParameter ControlID = "InqPhone" Name="InqPhone" PropertyName="Text" />
            <asp:ControlParameter ControlID = "radInqRel" Name="InqRelation" PropertyName="selectedvalue" />
            <asp:ControlParameter ControlID = "InitInqVia" Name="InqVia" PropertyName = "selectedvalue" />
            <asp:ControlParameter ControlID = "CareLevel" Name="InqCareLevel" PropertyName="selectedvalue" />
            <asp:ControlParameter ControlID = "ProgSelect" Name="InqProgram" PropertyName="selectedvalue" />     
        </InsertParameters>

提前谢谢你, 尼克

【问题讨论】:

    标签: asp.net vb.net


    【解决方案1】:

    在这个InsertPatientInfo_Inserted 处理程序中,您可以将输出参数的值指定为您的另一个名为InsInqInfoSqlDataSource 的默认值:

    Protected Sub InsertPatientInfo_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles InsertPatientInfo.Inserted
        Dim PatID As String = e.Command.Parameters("@PatID").Value
        MsgBox(PatID, MsgBoxStyle.Critical)
        ' Assign the output as the default for 
        InsInqInfo.InsertCommand("@PatID").DefaultValue = PatID
    End Sub
    

    我个人会将所有这些代码移到后面的代码中。

    【讨论】:

      【解决方案2】:

      点击此页面可查看有关类型化数据源及其参数的更多信息。 http://www.tek-tips.com/faqs.cfm?fid=7074

      http://msdn.microsoft.com/en-us/library/ms228051.aspx

      http://msdn.microsoft.com/en-us/library/z72eefad.aspx

      <asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" />
      

      检查此代码sn-p中的插入语句。

      <asp:sqlDataSource ID="EmployeeDetailsSqlDataSource" 
        SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID"
      
        InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName); 
                       SELECT @EmpID = SCOPE_IDENTITY()"
        UpdateCommand="UPDATE Employees SET LastName=@LastName, FirstName=@FirstName 
                         WHERE EmployeeID=@EmployeeID"
        DeleteCommand="DELETE Employees WHERE EmployeeID=@EmployeeID"
      
        ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
        OnInserted="EmployeeDetailsSqlDataSource_OnInserted"
        RunAt="server">
      
        <SelectParameters>
          <asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" />
        </SelectParameters>
      
        <InsertParameters>
          <asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" />
        </InsertParameters>
      
      </asp:sqlDataSource>
      

      【讨论】:

      • 我已经阅读了该教程,并且我相信我的代码与他的完全相似。问题是我无法在 .inserted 事件之外访问我的 @PatID。我将它放在 msgbox() 中并显示正确的 ID,但我无法将其用作具有不同 SqlDataSource 的 ,因此我可以将其用作另一个数据库表中的关系键。有没有办法做到这一点?
      猜你喜欢
      • 1970-01-01
      • 2011-10-08
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2011-04-21
      相关资源
      最近更新 更多