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

How to display record from SQL Server in a textbox by stored procedure

我有 table 叫 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 class 有这个代码:

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

我还有两个 classes:数据 class 和业务 class .

数据class代码:

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

和业务 class 代码:

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 文件中

方法代码写错了

不是reader

它是:

Public Function fillTable(ByRef cmd As SqlCommand, ByRef dt As DataTable) As Integer
    Dim retval As Integer = -1
    dt = New DataTable
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Connection = Me.Connection
    Dim da As New SqlDataAdapter(cmd)
    da.Fill(dt)

    If (Not dt Is Nothing) Then retval = dt.Rows.Count
    Return retval
End Function

按钮代码为:

p.Get_Prices_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

我想在txtitemid.text

中输入项目的ID

它将搜索此 ID,如果找到 return 返回其余记录

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

您正在 ToString 处理 Rows(全部),而不是连续的数据。当你在 DataRowCollection 上调用 ToString 时,它会给你 "System.Data.DataRowCollection".

您需要在与您的文本框匹配的行和列索引处提取数据。您的文本框名称与您的列名称不匹配,但做出假设,这样做:

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

当然,您应该检查您实际上至少返回了一行并且 none 的列值是空的,因为如果该列是 [=16=,则您不能调用 ToString ].

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

我还建议您在方法内部创建命令和连接,这样您就可以确保 Dispose() 它们(以及数据适配器),因为它们是一次性资源。那个或者你 class 需要实施 IDisposable 而这更复杂。