我通常用我的 DTO 做的事情类似于下面的 vb
Public Sub InsertSupplier(ByVal SupplierObject As Supplier) Implements ISupplierRepository.InsertSupplier
SupplierObject.SupplierID = MyBase.ExecuteNonQueryWithReturnValue("usp_Northwind_InsertSupplier", SupplierObject.CompanyName)
End Sub
使用类似于以下的我的存储过程
USE [Northwind]
GO
/****** Object: StoredProcedure [dbo].[usp_Northwind_InsertSupplier] Script Date: 1/1/2009 10:59:01 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_Northwind_InsertSupplier]
@CompanyName nvarchar(40)
AS
INSERT INTO dbo.Suppliers
VALUES (@CompanyName)
RETURN @@Identity
只是为了澄清我在基类中的 1 班轮做了什么(使用企业库仅供参考)
Public Function ExecuteNonQueryWithReturnValue(ByVal storedProcedureName As String, ByVal ParamArray parameterValues() As Object) As Object Implements Interfaces.IRepository(Of T).ExecuteNonQueryWithReturnValue
Dim Output As Object
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim dbCommand As Common.DbCommand = db.GetStoredProcCommand(storedProcedureName, parameterValues)
db.ExecuteNonQuery(dbCommand)
Output = CType(db.GetParameterValue(dbCommand, "@RETURN_VALUE"), Object)
Return Output
End Function