我有一个名为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
文件
答案 0 :(得分:1)
你ToString
Rows
(所有这些),而不是连续的数据。当您在ToString
上致电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
,那就更复杂了。
答案 1 :(得分:0)
我写了错误的方法代码
它不是读者
它是:
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
中输入item的id它将搜索此ID并返回记录的其余部分(如果找到)
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