나는 테이블이 있습니다 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
연속하지 데이터, (그들 모두). 당신이 호출 할 때 ToString
A의 DataRowCollection
그것은 당신에게 "System.Data.DataRowCollection"를 제공 할 것입니다.
텍스트 상자와 일치하는 행 및 열 인덱스에서 데이터를 추출해야합니다. 텍스트 상자 이름이 열 이름과 일치하지 않지만 가정하여 다음을 수행하십시오.
FrmManage_Items.txtwholesaleone.Text = dt.Rows(0)("FirstUnitWholeSalePrice").ToString
FrmManage_Items.txtcustomerone.Text = dt.Rows(0)("FirstUnitShopperPrice").ToString
'etc....
물론 열이이면 호출 할 수 없기 때문에 실제로 적어도 하나의 행을 다시 가져 왔고 열 값이 null이 ToString
아닌지 확인해야합니다 DBNull.Value
.
제쳐두고 매개 변수를 전달할 이유가 없습니다 ByRef
. 이것은 완전히 다른 논의이지만 실제로 객체에 대한 참조 (값이나 속성, 참조가 아님)를 변경하려는 경우가 아니면 잘못된 것입니다.
또한 명령과 연결을 메서드 내부에 생성 Dispose()
하여 데이터 어댑터와 함께 사용 가능한 리소스 이므로 확인할 수 있도록하는 것이 좋습니다 . 그것 또는 당신 클래스는 구현해야하며 IDisposable
더 복잡합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다