2

感谢您的帮助伙计们

我正在开发一个基于 Asp.net 的项目,我的要求是使用 MaterialType+ProductID 和 - 4digit 随机数的组合生成一个 PARTNO

注意:ProductID 是主键,也设置为输出参数

例如,如果材料类型为 500,生成的产品 ID 为 55,随机未生成 5434,则零件编号变为 555-5434

现在我的问题是如何将 partno 存储在同一张表中,我有点像那样尝试

        Connection.Open()
        Dim trn As SqlClient.SqlTransaction
        trn = Connection.BeginTransaction
        Using trn


            Dim sqlcode As New SqlParameter("@ProductID", Products.ProductID)
            sqlcode.Direction = ParameterDirection.InputOutput                 
            Using Command As New SqlCommand("Product_Write", Connection, trn)

                Command.CommandType = CommandType.StoredProcedure
                Command.Parameters.Add(sqlcode)                     Command.Parameters.AddWithValue("@MaterialType", Materialtype.MaterialTypeCode)
                Command.Parameters.AddWithValue("@CategoryID", category.CategoryId)
                Command.Parameters.AddWithValue("@ProductName", Products.ProductName)
                Command.Parameters.AddWithValue("@ProductDescription", Products.ProductDescription)
                Command.Parameters.AddWithValue("@ProductActive", Products.ProductActive)
                Command.Parameters.AddWithValue("@ProductImage", Products.ProductImage)
                Command.Parameters.AddWithValue("@PartNo", 0)


                Command.ExecuteNonQuery()
                Products.ProductID = CInt(sqlcode.Value)
                'Insert the part no 

                Dim random As New Random()
                Dim value As Integer = random.Next(9999)
                Dim PartNo As String = CType((Materialtype.MaterialTypeCode + Products.ProductID).ToString + "-" + value.ToString, String)


                'Dont know what to do 

                trn.Commit()

                Connection.Close()
            End Using
        End Using
    End Using
    Return Products
End Function




ALTER PROCEDURE [dbo].[Product_Write]
@ProductID bigint OUTPUT,
@MaterialType int,
@CategoryID bigint,
@ProductName VARCHAR(MAX),
@ProductDescription VARCHAR(MAX),
@ProductActive Bit,
@ProductImage VARCHAR(MAX),
@PartNo VARCHAR(30)
AS
IF (@ProductID=0)
BEGIN
INSERT INTO T_Product(
MaterialType,
CategoryID,
ProductName,
ProductDescription,
ProductActive,
ProductImage,
PartNo)
VALUES(
@MaterialType,
@CategoryID,
@ProductName,
@ProductDescription,
@ProductActive,
@ProductImage,
@PartNo)

SET @ProductID=SCOPE_IDENTITY()
END 
ELSE
 UPDATE T_Product SET
      MaterialType=@MaterialType,
      CategoryID=@CategoryID,
      ProductName=@ProductName,
      ProductDescription=@ProductDescription,
      ProductActive=@ProductActive,
      ProductImage=@ProductImage,
      PartNo=@PartNo
WHERE ProductID=@ProductID
                Please help me out

谢谢

4

1 回答 1

0

如果您可以更新存储过程,您将需要一些类似的东西

declare @id int
set @id = SCOPE_IDENTITY()
UPDATE <Table> Set PartNo = MaterialType + convert(varchar(max),@id) + convert(varchar(4),CEILING(9999*RAND())) Where ID = @id
于 2012-06-13T08:02:48.323 回答