如何通过存储过程在文本框中显示来自 SQL Server 的记录

塞勒姆

我有一张桌子叫ItemsPricesTbl

ItemID
FirstUnitWholeSalePrice
FirstUnitShopperPrice
FirstUnitDemotionsPrice
FirstUnitPriceDefault
SecondUnitWholeSalePrice
SecondUnitShopperPrice
SecondUnitDemotionsPrice
SecondUnitPriceDefault
ThirdUnitWholeSalePrice
ThirdUnitShopperPrice
ThirdUnitDemotionsPrice
ThirdUnitPriceDefault
DefaultPrice

获取数据的存储过程是:

ALTER PROCEDURE [dbo].[Get_Prices_Item_By_ID]
    @ItemID int
AS
BEGIN 
    SELECT
        ItemID, FirstUnitWholeSalePrice, FirstUnitShopperPrice, 
        FirstUnitDemotionsPrice, FirstUnitPriceDefault,
        SecondUnitWholeSalePrice, SecondUnitShopperPrice,
        SecondUnitDemotionsPrice, SecondUnitPriceDefault,
        ThirdUnitWholeSalePrice, ThirdUnitShopperPrice,
        ThirdUnitDemotionsPrice, ThirdUnitPriceDefault,
        DefaultPrice 
    FROM
        ItemsPricesTbl 
    WHERE
        ItemID = @ItemID
END

在 Vb 中,我有一个DatabaseManager包含以下代码类:

Private Function exeReader(ByRef cmd As SqlCommand, ByRef dr As SqlDataReader) As Integer
        Dim retval As Integer = -1
        cmd.Connection = Me.Connection
        Try
            If cmd.CommandType = CommandType.StoredProcedure Then
                Dim pr As New SqlParameter("@retval", SqlDbType.Int)
                pr.Direction = ParameterDirection.ReturnValue
                cmd.Parameters.Add(pr)
            End If
            If cmd.Connection.State = ConnectionState.Closed Then cmd.Connection.Open()
            dr = cmd.ExecuteReader()

            If cmd.CommandType = CommandType.StoredProcedure Then retval = cmd.Parameters("@retval").Value
        Catch ex As Exception
            Throw New Exception(ex.Message)
        Finally
            Me.close()
        End Try

        Return retval
    End Function

另外我有两个类:数据类和业务类。

数据类代码:

Friend Sub Get_Prices_Item_By_ID(ByRef dt As DataTable, ByVal ItemID As Integer)
    Dim cmd As New SqlCommand("Get_Prices_Item_By_ID")
    cmd.Parameters.Add("@ItemID", SqlDbType.Int).Value = ItemID
    dm.fillTable(cmd, dt)
End Sub

和商务舱代码:

Public Function Get_Prices_Item_By_ID(ByVal ItemID As Integer) As DataTable
    Dim dt As New DataTable
    p.Get_Prices_Item_By_ID(dt, ItemID)
    Return dt
End Function

我必须在搜索按钮中输入什么代码?

我试过这个:

p.Get_Item_By_ID(FrmManage_Items.txtitemid.Text)
                FrmManage_Items.txtwholesaleone.Text = dt.Rows.ToString
                FrmManage_Items.txtcustomerone.Text = dt.Rows.ToString
                FrmManage_Items.txtsaleone.Text = dt.Rows.ToString
                FrmManage_Items.RadioButton4.Checked = dt.Rows.ToString
                FrmManage_Items.txtwholesaletwo.Text = dt.Rows.ToString
                FrmManage_Items.txtcustomertwo.Text = dt.Rows.ToString
                FrmManage_Items.txtsaletwo.Text = dt.Rows.ToString
                FrmManage_Items.RadioButton5.Checked = dt.Rows.ToString
                FrmManage_Items.txtwholesalethird.Text = dt.Rows.ToString
                FrmManage_Items.txtcustomerthird.Text = dt.Rows.ToString
                FrmManage_Items.txtsalethird.Text = dt.Rows.ToString
                FrmManage_Items.RadioButton6.Checked = dt.Rows.ToString
                FrmManage_Items.TextBox20.Text = dt.Rows.ToString

但我没有得到记录 - 但我在每个文本框中得到的错误:

System.Data.DataRowCollection

注意:连接在app.config文件中

编码器

您正在ToString处理Rows(所有这些),而不是连续的数据。当您调用ToStringa 时,DataRowCollection它会给您“System.Data.DataRowCollection”。

您需要在与文本框匹配的行和列索引处提取数据。您的文本框名称与您的列名称不匹配,但进行假设,请执行以下操作:

FrmManage_Items.txtwholesaleone.Text = dt.Rows(0)("FirstUnitWholeSalePrice").ToString
FrmManage_Items.txtcustomerone.Text = dt.Rows(0)("FirstUnitShopperPrice").ToString
'etc....

当然,您应该检查您实际上至少返回了一行并且没有任何列值为空,因为ToString如果列是DBNull.Value.

顺便说一句,没有理由传递您的参数ByRef这是一个完全不同的讨论,但除非您确实打算更改对对象的引用(不是它的值或属性,而是它的引用),否则这是错误的。

我还建议您在方法中创建命令和连接,以便您可以确定Dispose()它们(以及数据适配器),因为它们是一次性资源。要么是你,要么是你的班级需要实现IDisposable,那就更复杂了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章