SQL Server根据combobox.selectedvalue选择存储过程

发布于 2024-08-28 11:14:09 字数 2876 浏览 3 评论 0原文

为了根据组合框的选定值填充数据网格视图,我尝试创建一个存储过程。但是,由于我不是 100% 确定我在做什么,根据存储过程末尾的 WHERE 语句,它要么返回表中的所有内容,要么什么也不返回。

这是我的类中的内容:

Public Function GetAankoopDetails(ByRef DisplayMember As String, ByRef ValueMember As String) As DataTable

    DisplayMember = "AankoopDetailsID"
    ValueMember = "AankoopDetailsID"

    If DS.Tables.Count > 0 Then
        DS.Tables.Remove(DT)
    End If

    DT = DAC.ExecuteDataTable(My.Resources.S_AankoopDetails, _Result, _
                              DAC.Parameter(Const_AankoopID, AankoopID), _
                              DAC.Parameter("@ReturnValue", 0))

    DS.Tables.Add(DT)

    Return DT
End Function

Public Function GetAankoopDetails() As DataTable

    If DS.Tables.Count > 0 Then
        DS.Tables.Remove(DT)
    End If

    DT = DAC.ExecuteDataTable(My.Resources.S_AankoopDetails, _Result, _
                              DAC.Parameter(Const_AankoopID, AankoopID), _
                              DAC.Parameter("@ReturnValue", 0))

    DS.Tables.Add(DT)
    Return DT
End Function

这是我为了填充 datagridview 而编写的表单背后的代码中的函数:

  Private Sub GridAankoopDetails_Fill()
    Try
        Me.Cursor = Cursors.WaitCursor
        dgvAankoopDetails.DataSource = Nothing
        _clsAankoopDetails.AankoopDetailsID = cboKeuze.SelectedValue
        dgvAankoopDetails.DataSource = _clsAankoopDetails.GetAankoopDetails



    Catch ex As Exception
        MessageBox.Show("An error occurred while trying to fill the data grid: " & ex.Message, "Oops!", MessageBoxButtons.OK)
    Finally
        Me.Cursor = Cursors.Default
    End Try
End Sub

最后,这是我的存储过程:(请注意,我不确定我在做什么在这里做)

USE [Budget]
GO
/****** Object:  StoredProcedure [dbo].[S_AankoopDetails]    Script Date: 04/12/2010 03:10:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[S_AankoopDetails]
(
 @AankoopID int

)
AS

SELECT     dbo.tblAankoopDetails.AankoopDetailsID, dbo.tblAankoopDetails.AankoopID, dbo.tblAankoopDetails.ArtikelID, dbo.tblAankoopDetails.Aantal, 
                      dbo.tblAankoopDetails.Prijs, dbo.tblAankoopDetails.Korting, dbo.tblAankoopDetails.SoortKorting, dbo.tblAankoopDetails.UitgavenDeelGroepID
FROM         dbo.tblAankoopDetails INNER JOIN
                      dbo.tblAankoop ON dbo.tblAankoopDetails.AankoopID = dbo.tblAankoop.AankoopID INNER JOIN
                      dbo.tblArtikel ON dbo.tblAankoopDetails.ArtikelID = dbo.tblArtikel.ArtikelID INNER JOIN
                      dbo.tblUitgavenDeelGroep ON dbo.tblAankoopDetails.UitgavenDeelGroepID = dbo.tblUitgavenDeelGroep.UitgavenDeelGroepID
WHERE dbo.tblAankoopDetails.Deleted = 0 and dbo.tblAankoopDetails.AankoopID = @AankoopID

ORDER BY AankoopID

有谁知道我需要做什么来解决这个问题?我想这取决于存储过程的 WHERE 部分,但我需要一种方法将组合框的选定值传递到 @AankoopID 参数中。

任何帮助将不胜感激。

亲切的问候 杰伊

In order to fill a datagridview according to the selectedvalue of a combobox I've tried creating a stored procedure. However, as I'm not 100% sure what I'm doing, depending on the WHERE statement at the end of my stored procedure, it either returns everything within the table or nothing at all.

This is what's in my class:

Public Function GetAankoopDetails(ByRef DisplayMember As String, ByRef ValueMember As String) As DataTable

    DisplayMember = "AankoopDetailsID"
    ValueMember = "AankoopDetailsID"

    If DS.Tables.Count > 0 Then
        DS.Tables.Remove(DT)
    End If

    DT = DAC.ExecuteDataTable(My.Resources.S_AankoopDetails, _Result, _
                              DAC.Parameter(Const_AankoopID, AankoopID), _
                              DAC.Parameter("@ReturnValue", 0))

    DS.Tables.Add(DT)

    Return DT
End Function

Public Function GetAankoopDetails() As DataTable

    If DS.Tables.Count > 0 Then
        DS.Tables.Remove(DT)
    End If

    DT = DAC.ExecuteDataTable(My.Resources.S_AankoopDetails, _Result, _
                              DAC.Parameter(Const_AankoopID, AankoopID), _
                              DAC.Parameter("@ReturnValue", 0))

    DS.Tables.Add(DT)
    Return DT
End Function

This is the function in the code behind the form I've written in order to fill the datagridview:

  Private Sub GridAankoopDetails_Fill()
    Try
        Me.Cursor = Cursors.WaitCursor
        dgvAankoopDetails.DataSource = Nothing
        _clsAankoopDetails.AankoopDetailsID = cboKeuze.SelectedValue
        dgvAankoopDetails.DataSource = _clsAankoopDetails.GetAankoopDetails



    Catch ex As Exception
        MessageBox.Show("An error occurred while trying to fill the data grid: " & ex.Message, "Oops!", MessageBoxButtons.OK)
    Finally
        Me.Cursor = Cursors.Default
    End Try
End Sub

And finally, this is my stored procedure: (do note that I'm not sure what I'm doing here)

USE [Budget]
GO
/****** Object:  StoredProcedure [dbo].[S_AankoopDetails]    Script Date: 04/12/2010 03:10:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[S_AankoopDetails]
(
 @AankoopID int

)
AS

SELECT     dbo.tblAankoopDetails.AankoopDetailsID, dbo.tblAankoopDetails.AankoopID, dbo.tblAankoopDetails.ArtikelID, dbo.tblAankoopDetails.Aantal, 
                      dbo.tblAankoopDetails.Prijs, dbo.tblAankoopDetails.Korting, dbo.tblAankoopDetails.SoortKorting, dbo.tblAankoopDetails.UitgavenDeelGroepID
FROM         dbo.tblAankoopDetails INNER JOIN
                      dbo.tblAankoop ON dbo.tblAankoopDetails.AankoopID = dbo.tblAankoop.AankoopID INNER JOIN
                      dbo.tblArtikel ON dbo.tblAankoopDetails.ArtikelID = dbo.tblArtikel.ArtikelID INNER JOIN
                      dbo.tblUitgavenDeelGroep ON dbo.tblAankoopDetails.UitgavenDeelGroepID = dbo.tblUitgavenDeelGroep.UitgavenDeelGroepID
WHERE dbo.tblAankoopDetails.Deleted = 0 and dbo.tblAankoopDetails.AankoopID = @AankoopID

ORDER BY AankoopID

Does anyone know what I need to do to resolve this? I guess it's down to the WHERE part of the stored procedure, but I need a way to pass the selectedvalue of the combobox into the @AankoopID parameter.

Any help would be greatly appreciated.

Kind regards
Jay

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

女皇必胜 2024-09-04 11:14:09

包含 WHERE 子句的存储过程代码看起来不错。您应该在 ORDER 子句中指定一个表:

ORDER by dbo.tblAankoopDetails.AankoopId

但这并不能解释为什么您获得所有行或没有行。您可能想要查看表的实际内容,确保其配置正确,NULL 值不会让您陷入困境,等等。

The stored procedure code, WHERE clause included, looks ok. You should specify a table in the ORDER clause:

ORDER by dbo.tblAankoopDetails.AankoopId

but that wouldn't explain why you get all or no rows. You might want to review the actual contents of the tables, make sure it's properly configured, NULL values aren't tripping you up, etc.

榕城若虚 2024-09-04 11:14:09

Philip、David,

我通过将 GridAankoopDetails_Fill 子中的一行从更改

_clsAankoopDetails.AankoopDetailsID = cboKeuze.SelectedValue

_clsAankoopDetails.AankoopID = cboKeuze.SelectedValue

This 解决了这个问题,因为传递的是 AankoopID 参数,而不是 AankoopDetailsID。感谢您确认存储过程没有问题,从而缩小了问题的搜索范围。

另外,菲利普,谢谢你让我看看参数中传递的内容,这最终解决了问题。

数据网格现在根据组合框的选定值进行更新。

最美好的祝愿,

杰伊

Philip, David,

I've resolved the issue by changing one line in the GridAankoopDetails_Fill sub from

_clsAankoopDetails.AankoopDetailsID = cboKeuze.SelectedValue

to

_clsAankoopDetails.AankoopID = cboKeuze.SelectedValue

This because rather than AankoopDetailsID, the AankoopID parameter gets passed along. Thank you for confirming that the stored procedure was OK, narrowing down the search for the problem.

Also Philip, thank you for making me look at what is being passed along in the parameter, this ultimately lead to resolving the issue.

The data grid now updates according to the combobox' selected value.

Best wishes,

Jay

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文