Firebird .NET提供程序中是否存在可更新结果集和记录集?
问题描述:
我昨天试过Firebird和.NET Provider。 Firebird肯定会击败SQL Compact Edition。我很惊讶它的功能和精度。但是当我无法在.NET提供程序中找到可更新的RecordSet/ResultSet功能时,我感到很难过。 FBResultSet不包含任何内容,没有行添加,创建,修改,没有任何内容。任何人都知道这个功能是否存在于Firebird的Ado .Net提供程序中,因为没有文档。我想获得帮助,因为我正在等待将Firebird整合到我的免费软件应用程序中。另外,如果有替换函数来实现可更新的RecordSet,ResultSet。请帮帮我。Firebird .NET提供程序中是否存在可更新结果集和记录集?
问候。
答
我去过那里了,做完了,拿到了T恤。所以,当我感到你的痛苦时,我会给你一些应该帮助你的代码。
我用存储过程(sprocs)来做任何记录更新addtion等,然后我写了一个类包装来调用sprocs。
在这里输入的代码
所以,用此代码为存储过程:
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
你会与此数据库包装访问: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
这里是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
这是很多代码,但我不得不这样做,很难做到这一点。我希望你可以用这个来弥补你的差距来起跑。火鸟真的是一个伟大的数据库。
感谢您的源代码。我不知道在哪里放置什么,以及如何调用特定的函数来更新DataTable中的任何行。 – user529948 2010-12-04 10:08:10