如何显示从SQL到文本框的记录 [英] How to display record from SQL to textbox

查看:56
本文介绍了如何显示从SQL到文本框的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...



我有桌子叫ItemsPricesTbl



包含:



 ItemID 
FirstUnitWholeSalePrice
FirstUnitShopperPrice
FirstUnitDemotionsPrice
FirstUnitPriceDefult
SecondUnitWholeSalePrice
SecondUnitShopperPrice
SecondUnitDemotionsPrice
SecondUnitPriceDefult
ThirdUnitWholeSalePrice
ThirdUnitShopperPrice
ThirdUnitDemotionsPrice
ThirdUnitPriceDefult
DefultPrice







存储的Porc是:



  ALTER   PROCEDURE  [dbo]。[Get_Prices_Item_By_ID] 

@ItemID int

as

开始

选择 ItemID,FirstUnitWholeSalePrice,FirstUn itShopperPrice,FirstUnitDemotionsPrice,FirstUnitPriceDefult,SecondUnitWholeSalePrice,SecondUnitShopperPrice,SecondUnitDemotionsPrice,SecondUnitPriceDefult,ThirdUnitWholeSalePrice,ThirdUnitShopperPrice,ThirdUnitDemotionsPrice,ThirdUnitPriceDefult,DefultPrice <跨度类= 代码关键字>从 ItemsPricesTbl <跨度类= 代码关键字>其中 ItemID = @ ItemID

结束





in Vb我有数据库管理员类有这个代码:





 私有 函数 exeReader( ByRef  cmd  As  SqlCommand, ByRef  dr  As  SqlDataReader) As  整数 
Dim retval 作为 整数 = -1
cmd.Connection = 。连接
尝试
如果 cmd.CommandType = CommandType.StoredProcedure 然后
Dim pr As SqlParameter( @ retval,SqlDbType.Int)
pr.Direction = ParameterDirection.ReturnValue
cmd.Parameters.Add(pr)
End 如果
如果 cmd.Connection.State = ConnectionState.Closed 那么 cmd.Connection.Open()
dr = cmd.ExecuteReader()

如果 cmd.CommandType = CommandType.StoredProcedure 那么 retval = cmd.Parameters( @ retval)。值
Catch ex As 异常
抛出 异常( ex.Message)
最后
.close()
结束 尝试

返回 retval
结束 功能









我还有两个班级数据类和商务舱。



数据类代码:



<前la ng =vb> 朋友 功能 Get_Prices_Item_By_ID( ByRef cmd 作为 SqlCommand, ByRef dr As SqlDataReader, ByVal ItemID 作为 整数 As 整数
Dim retval 作为 字符串
cmd = SqlCommand( Get_Prices_Item_By_ID
daa.SelectCommand.Parameters.AddWithValue( @ FirstUnitWholeSalePrice,SqlDbType。十进制
daa.SelectCommand.Parameters.AddWithValue( @ FirstUnitShopperPrice,SqlDbType。十进制
daa.SelectCommand.Parameters.AddWithValue( @ FirstUnitDemotionsPrice,SqlDbType。 Decimal
daa.SelectCommand.Parameters.AddWithValue( @ FirstUnitPriceDefult,SqlDbType.Bit)
daa.SelectCommand.Parameters.AddWithValue( @ SecondUnitWholeSalePrice,SqlDbType。 Decimal
daa .SelectCommand.Parameters.AddWithValue( @ SecondUnitShopperPrice,SqlDbType。十进制
daa.SelectCommand.Parameters.AddWithValue( @ SecondUnitDemotionsPrice,SqlDbType。 十进制
daa.SelectCommand.Parameters.AddWithValue( @ SecondUnitPriceDefult,SqlDbType.Bit)
daa.SelectCommand.Parameters.AddWithValue( @ThirdUnitWholeSalePrice,SqlDbType。十进制
daa.SelectCommand.Parameters.AddWithValue( @ ThirdUnitShopperPrice,SqlDbType。 Decimal
daa.SelectCommand.Parameters .AddWithValue( @ ThirdUnitDemotionsPrice,SqlDbType。 Decimal
daa.SelectCommand.Parameters.AddWithValue( @ ThirdUnitPriceDefult,SqlDbType.Bit)
daa .SelectCommand.Parameters.AddWithValue( @ DefultPrice,SqlDbType.Int)
dr = cmd.ExecuteReader()
返回 retval
结束 功能







和商务舱代码:



 公共 功能 Get_Prices_Item_By_ID ( ByVal  ItemID 作为 整数 As  DataTable 
Dim dt As <跨班=code-keyword>新 DataTable
p.Get_Prices_Item_By_ID(dt)
返回 dt
结束 功能







我必须在搜索按钮中添加什么代码?

如何显示从SQL到TextBox的记录





我是什么尝试过:



存储的Porc是:

< pre lang =SQL > ALTER PROCEDURE [dbo]。[Get_Prices_Item_By_ID]

@ItemID int

as

begin

选择ItemID ,FirstUnitWholeSalePrice,FirstUnitShopperPrice,FirstUnitDemotionsPrice,FirstUnitPriceDefult,SecondUnitWholeSalePrice,SecondUnitShopperPrice,SecondUnitDemotionsPrice,SecondUnitPriceDefult,ThirdUnitWholeSalePrice,ThirdUnitShopperPrice,ThirdUnitDemotionsPrice,ThirdUnitPriceDefult,DefultPrice从ItemsPricesTbl哪里的ItemID = @的ItemID




Vb中的
我有这个代码的DatabaseManager类:





 私有 功能 exeReader( ByRef  cmd 作为 SqlCommand , ByRef  dr  As  SqlDataReader) As  < span class =code-keyword>整数 
Dim retval As < span class =code-keyword> Integer = -1
cmd.Connection = Me .Connection
尝试
如果 cmd.CommandType = CommandType.StoredProcedure 那么
Dim pr As SqlParameter( @ retval,SqlDbType.Int)
pr.Direction = ParameterDirection .ReturnValue
cmd.Parameters.Add(pr)
结束 如果
如果 cmd.Connection.State = ConnectionState.Closed 然后 cmd.Connection.Open()
dr = cmd.ExecuteReader()

如果 cmd.CommandType = CommandType.StoredProcedure 然后 retval = cmd.Parameters( @retval)。值
Catch ex As 例外
投掷 例外(ex.Message)
最后
.close()
结束 尝试

返回 retval
结束 功能









我还有两个类数据类和业务类。



数据类代码:



 朋友 功能 Get_Prices_Item_By_ID( ByRef  cmd  As  SqlCommand, ByRef  dr 作为 SqlDataReader, ByVal  ItemID 作为 整数作为 整数 
Dim retval As String
cmd = SqlCommand( Get_Prices_Item_By_ID
daa.SelectCommand.Parameters.AddWithValue( @ FirstUnitWholeSalePrice,SqlDbType。 Decimal
daa.SelectCommand.Parameters.AddWithValue( @ FirstUnitShopperPrice,SqlDbType。 Decimal
daa.SelectCommand.Parameters.AddWithValue( @ FirstUnitDemotionsPrice,SqlDbType。十进制
daa.SelectCommand.Parameters.AddWithValue( @ FirstUnitPriceDefult,SqlDbType.Bit)
daa.SelectCommand.Parameters.AddWithValue( @ SecondUnitWholeSalePrice , SqlDbType。 Decimal
daa.SelectCommand.Parameters.AddWithValue( @ SecondUnitShopperPrice,SqlDbType。 Decimal
daa.SelectCommand.Parameters.AddWithValue( @ SecondUnitDemotionsPrice,SqlDbType。 Decimal
daa。 SelectCommand.Parameters.AddWithValue( @ SecondUnitPriceDefult,SqlDbType.Bit)
daa.SelectCommand .Parameters.AddWithValue( @ ThirdUnitWholeSalePrice,SqlDbType。十进制
daa.SelectCommand.Parameters.AddWithValue( @ ThirdUnitShopperPrice ,SqlDbType。< span class =code-keyword> Decimal

daa.SelectCommand.Parameters.AddWithValue( @ThirdUnitDemotionsPrice,SqlDbType。十进制
daa.SelectCommand.Parameters.AddWithValue( @ ThirdUnitPriceDefult,SqlDbType.Bit)
daa.SelectCommand.Parameters.AddWithValue( @ DefultPrice,SqlDbType.Int)
dr = cmd.ExecuteReader()
返回 retval
结束 功能







和商务舱代码:



 公共 功能 Get_Prices_Item_By_ID( ByVal  ItemID 作为 整数作为 DataTable 
Dim dt As DataTable
p.Get_Prices_Item_By_ID(dt)
返回 dt
结束 功能

解决方案

您需要做的就是在现有的SqlReader代码中添加第二个参数:

  Dim  pr 作为  SqlParameter(  @ retval,SqlDbType.Int)
pr.Direction = ParameterDirection.ReturnValue
cmd.Parameters.Add(pr)
cmd.Parameters。 AddWithValue( @ ItemID,idOfTheItemYouWantToSearchFor)

然后,您的读者可以获取值并将其添加到文本框中。


Hi ...

I have Table Called "ItemsPricesTbl"

Contain :

ItemID
FirstUnitWholeSalePrice
FirstUnitShopperPrice
FirstUnitDemotionsPrice
FirstUnitPriceDefult
SecondUnitWholeSalePrice
SecondUnitShopperPrice
SecondUnitDemotionsPrice
SecondUnitPriceDefult
ThirdUnitWholeSalePrice
ThirdUnitShopperPrice
ThirdUnitDemotionsPrice
ThirdUnitPriceDefult
DefultPrice




The Stored Porc is :

ALTER PROCEDURE [dbo].[Get_Prices_Item_By_ID]

@ItemID int

as

begin

Select ItemID,FirstUnitWholeSalePrice,FirstUnitShopperPrice,FirstUnitDemotionsPrice,FirstUnitPriceDefult,SecondUnitWholeSalePrice,SecondUnitShopperPrice,SecondUnitDemotionsPrice,SecondUnitPriceDefult,ThirdUnitWholeSalePrice,ThirdUnitShopperPrice,ThirdUnitDemotionsPrice,ThirdUnitPriceDefult,DefultPrice from ItemsPricesTbl Where ItemID=@ItemID

End



in Vb i have DatabaseManager Class Which Have this Code :


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





Also I have two classes The Data Class And Business Class .

Data Class Code :

Friend Function Get_Prices_Item_By_ID(ByRef cmd As SqlCommand, ByRef dr As SqlDataReader, ByVal ItemID As Integer) As Integer
            Dim retval As String
            cmd = New SqlCommand("Get_Prices_Item_By_ID")
            daa.SelectCommand.Parameters.AddWithValue("@FirstUnitWholeSalePrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@FirstUnitShopperPrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@FirstUnitDemotionsPrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@FirstUnitPriceDefult", SqlDbType.Bit)
            daa.SelectCommand.Parameters.AddWithValue("@SecondUnitWholeSalePrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@SecondUnitShopperPrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@SecondUnitDemotionsPrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@SecondUnitPriceDefult", SqlDbType.Bit)
            daa.SelectCommand.Parameters.AddWithValue("@ThirdUnitWholeSalePrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@ThirdUnitShopperPrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@ThirdUnitDemotionsPrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@ThirdUnitPriceDefult", SqlDbType.Bit)
            daa.SelectCommand.Parameters.AddWithValue("@DefultPrice", SqlDbType.Int)
            dr = cmd.ExecuteReader()
            Return retval
        End Function




And Business Class Code :

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)
    Return dt
End Function




What Code I have to Put in search Button ?

How To Display Record From SQL To TextBox 



What I have tried:

The Stored Porc is : 

<pre lang="SQL">ALTER PROCEDURE [dbo].[Get_Prices_Item_By_ID]

@ItemID int

as

begin

Select ItemID,FirstUnitWholeSalePrice,FirstUnitShopperPrice,FirstUnitDemotionsPrice,FirstUnitPriceDefult,SecondUnitWholeSalePrice,SecondUnitShopperPrice,SecondUnitDemotionsPrice,SecondUnitPriceDefult,ThirdUnitWholeSalePrice,ThirdUnitShopperPrice,ThirdUnitDemotionsPrice,ThirdUnitPriceDefult,DefultPrice from ItemsPricesTbl Where ItemID=@ItemID

End



in Vb i have DatabaseManager Class Which Have this Code :


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





Also I have two classes The Data Class And Business Class .

Data Class Code :

Friend Function Get_Prices_Item_By_ID(ByRef cmd As SqlCommand, ByRef dr As SqlDataReader, ByVal ItemID As Integer) As Integer
            Dim retval As String
            cmd = New SqlCommand("Get_Prices_Item_By_ID")
            daa.SelectCommand.Parameters.AddWithValue("@FirstUnitWholeSalePrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@FirstUnitShopperPrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@FirstUnitDemotionsPrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@FirstUnitPriceDefult", SqlDbType.Bit)
            daa.SelectCommand.Parameters.AddWithValue("@SecondUnitWholeSalePrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@SecondUnitShopperPrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@SecondUnitDemotionsPrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@SecondUnitPriceDefult", SqlDbType.Bit)
            daa.SelectCommand.Parameters.AddWithValue("@ThirdUnitWholeSalePrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@ThirdUnitShopperPrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@ThirdUnitDemotionsPrice", SqlDbType.Decimal)
            daa.SelectCommand.Parameters.AddWithValue("@ThirdUnitPriceDefult", SqlDbType.Bit)
            daa.SelectCommand.Parameters.AddWithValue("@DefultPrice", SqlDbType.Int)
            dr = cmd.ExecuteReader()
            Return retval
        End Function




And Business Class Code :

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)
    Return dt
End Function

解决方案

Pretty much all you have to do is add a second parameter to your existing SqlReader code:

Dim pr As New SqlParameter("@retval", SqlDbType.Int)
pr.Direction = ParameterDirection.ReturnValue
cmd.Parameters.Add(pr)
cmd.Parameters.AddWithValue("@ItemID", idOfTheItemYouWantToSearchFor)

Your reader can then fetch the values and add them to your textbox.


这篇关于如何显示从SQL到文本框的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆