J'ai été là, fait cela, et j'ai eu le t-shirt. Alors, en ressentant votre douleur, je vais vous donner un code qui devrait vous aider.
J'ai utilisé des procédures stockées (sprocs) pour effectuer une mise à jour d'enregistrement, etc. Ensuite, j'ai écrit un wrapper de classe pour appeler les sprocs.
Entrez le code ici
Donc, avec ce code pour le sproc:
ALTER PROCEDURE SP_IU_BATCH (
AUTYPE INTEGER,
BATCHID INTEGER,
MAT_BATCHID INTEGER,
DOS DATE,
FACILITYID INTEGER)
RETURNS (
RTNIDX INTEGER)
AS
BEGIN
IF (:AUTYPE = 0) THEN
BEGIN
FOR
SELECT GEN_ID(GEN_BATCH_ID,1)
FROM RDB$DATABASE
INTO
:RTNIDX
DO
BEGIN
INSERT INTO BATCH (BATCHID, MAT_BATCHID, DOS, FACILITYID)
VALUES (:RTNIDX, :MAT_BATCHID, :DOS, :FACILITYID);
END
END
ELSE
BEGIN
UPDATE BATCH SET MAT_BATCHID=COALESCE(:MAT_BATCHID, MAT_BATCHID), DOS=COALESCE(:DOS, DOS),
FACILITYID=COALESCE(:FACILITYID, FACILITYID)
WHERE BATCHID=:BATCHID;
END
END
Vous serait-il accès à cette enveloppe de base de données: myfbdb.vb
Imports FirebirdSql.Data.Firebird
Imports FirebirdSql.Data.Firebird.Services
Imports FirebirdSql.Data.Firebird.Isql
Imports System.IO
Public Class myfbdb
#Region " Private Variables "
Private dbConn As FbConnection = Nothing, dbBatch As Batch = Nothing
Private Const dbName As String = "yourdatabasename.fdb", dbUser As String = "SYSDBA", dbPass As String = "yourpassword"
#End Region
#Region " Private Methods "
Private Sub Connect()
If dbConn Is Nothing Then
Try
dbConn = New FbConnection(GetConnString)
Catch ex As FbException
RaiseError(ex)
End Try
End If
End Sub
Private Sub Initialize()
dbBatch = New Batch
dbBatch.dbConn = Me
End Sub
#End Region
#Region " Private Functions "
Private Function GetConnString() As String
Dim rtnString As String = Nothing
Dim fbCSB As New FbConnectionStringBuilder
fbCSB.Database = dbName
fbCSB.Password = dbPass
fbCSB.UserID = dbUser
fbCSB.ServerType = 1
rtnString = fbCSB.ToString
fbCSB = Nothing
Return rtnString
End Function
#End Region
#Region " Public Properties "
Public ReadOnly Property cmdb() As FbConnection
Get
Return dbConn
End Get
End Property
Public ReadOnly Property cmBatch() As Batch
Get
Return dbBatch
End Get
End Property
End Property
#End Region
#Region " Public Methods "
#Region " New "
Public Sub New()
Connect()
Initialize()
End Sub
#End Region
#Region " Finalize "
Protected Overrides Sub Finalize()
MyBase.Finalize()
Close()
Unload()
End Sub
#End Region
#Region " Unload "
Public Sub Unload()
If Not dbConn Is Nothing Then
If dbConn.State = Data.ConnectionState.Open Then
dbConn.Close()
End If
dbConn = Nothing
End If
UnInitialize()
End Sub
#End Region
#Region " Open/Close "
Public Sub Open()
Try
If Not dbConn Is Nothing Then
If dbConn.State = Data.ConnectionState.Open Then
dbConn.Close()
End If
dbConn.Open()
End If
Catch ex As Exception
RaiseError(ex)
End Try
End Sub
Public Sub Close()
If Not dbConn Is Nothing Then
dbConn.Close()
End If
End Sub
#End Region
#End Region
End Class
Et est ici la Lot d'emballage pour le Sproc:
Imports FirebirdSql.Data.Firebird
Imports FirebirdSql.Data.Firebird.Services
Imports FirebirdSql.Data.Firebird.Isql
Public Class Batch
#Region " Private Variables "
Private clsCM As myfbdb
#End Region
#Region " Private Enums "
Private Enum AUType
Add = 0
Update = 1
End Enum
#End Region
#Region " Stored Procedures "
#Region " Add/Update "
Private Function AU_Batch(ByVal itmBatch As cmdbType_Batch, ByVal au As AUType) As Integer
Try
Dim rtnIndex As Integer = -1
'Create Temporary Command
Dim tmpTrans As FbTransaction = clsCM.cmdb.BeginTransaction
Dim tmpSQLCommand As FbCommand = New FbCommand("SP_IU_BATCH", clsCM.cmdb, tmpTrans)
tmpSQLCommand.CommandType = Data.CommandType.StoredProcedure
'Add Parameters
Dim prmAUType As FbParameter = tmpSQLCommand.Parameters.Add("@AUTYPE", FbDbType.Integer)
Dim prmBatchID As FbParameter = tmpSQLCommand.Parameters.Add("@BATCHID", FbDbType.Integer)
Dim prmMAT_BatchID As FbParameter = tmpSQLCommand.Parameters.Add("@MAT_BATCHID", FbDbType.Integer)
Dim prmDOS As FbParameter = tmpSQLCommand.Parameters.Add("@DOS", FbDbType.Date)
Dim prmFacilityName As FbParameter = tmpSQLCommand.Parameters.Add("@FACILITYNAME", FbDbType.VarChar, 250)
Dim prmFacilityID As FbParameter = tmpSQLCommand.Parameters.Add("@FACILITYID", FbDbType.Integer)
Dim prmRtnIdx As FbParameter = tmpSQLCommand.Parameters.Add("@RTNIDX", FbDbType.Integer)
'Specify Output Parameters
prmRtnIdx.Direction = Data.ParameterDirection.Output
'Set the Parameter Values
With itmBatch
prmAUType.Value = CInt(au)
prmBatchID.Value = .BatchID
prmMAT_BatchID.Value = .MAT_BatchID
prmDOS.Value = .DOS
prmFacilityName.Value = clsCM.enc.EncryptString128Bit(.FacilityName, Crypt_Text(clsCM.ivKey))
prmFacilityID.Value = .FacilityID
End With
'Execute the Stored Procedure
tmpSQLCommand.ExecuteNonQuery()
If au = AUType.Add Then
rtnIndex = prmRtnIdx.Value
End If
tmpTrans.Commit()
'Clean up
tmpSQLCommand = Nothing
Return rtnIndex
Catch ex As Exception
RaiseError(ex)
Return Nothing
End Try
End Function
#End Region
#Region " Public Functions "
Public Function AddBatch(ByVal itmBatch As cmdbType_Batch) As Integer
Try
clsCM.Open()
Dim rtnInteger As Integer = AU_Batch(itmBatch, AUType.Add)
clsCM.Close()
Return rtnInteger
Catch ex As Exception
RaiseError(ex)
clsCM.Close()
Return Nothing
End Try
End Function
Public Function UpdateBatch(ByVal itmBatch As cmdbType_Batch) As Integer
Try
clsCM.Open()
Dim rtnInteger As Integer = AU_Batch(itmBatch, AUType.Update)
clsCM.Close()
Return rtnInteger
Catch ex As Exception
RaiseError(ex)
clsCM.Close()
Return Nothing
End Try
End Function
#End Region
#Region " Public Properties "
Public WriteOnly Property dbConn() As CodingModuleDB
Set(ByVal value As CodingModuleDB)
clsCM = value
End Set
End Property
#End Region
End Class
C'était beaucoup de code, mais je devais le faire à la dure et ça sentait mauvais. J'espère que vous pourrez l'utiliser pour combler votre retard et vous lancer. Firebird est vraiment une excellente base de données.
Merci pour le code source. Je ne sais pas quoi mettre où, et comment appeler une fonction spécifique pour mettre à jour n'importe quelle ligne du DataTable. – user529948