473,804 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# File + DB Transaction

I have the following requirements and am looking to make this into a c#
transaction.

1. Read from a file into a memory stream
2. Insert the stream into a DB
3. Delete the file

Is it possible to perform a transaction on both a File action and a DB
insert?

A transaction will ensure that if an error occurs in step 3, the insert will
not commit, etc. All three steps must succeed, otherwise the transaction
should rollback and notify of an error.

Any assistance is appreciated.

Thanks

Oct 29 '08 #1
2 3977
Chris Fink schrieb:
1. Read from a file into a memory stream
2. Insert the stream into a DB
3. Delete the file
If you want to have all of that in ONE transaction...
You can use the transaction-object provided by your database provider
and wrap the commit()/rollback() into a try/catch.

With the Firebird-Provider it would look similar to this:

(Conn01 is a valid and open connection...)

FBTransaction Transaction1 = Conn01.BeginTra nsaction();

try
{
... read file into memory stream
... Insert the stream into DB
... Delete the file
Transaction1.Co mmit()
}
catch
{
Transaction1.Ro llback()
... show errormessages
}
I would consider, though, to put at least the "read file into stream"-
part outside the database transaction. Why bother the database with a
transaction if you probably can't read or even find the file.
Oct 29 '08 #2
Chris Fink wrote:
I have the following requirements and am looking to make this into a c#
transaction.

1. Read from a file into a memory stream
2. Insert the stream into a DB
3. Delete the file

Is it possible to perform a transaction on both a File action and a DB
insert?

A transaction will ensure that if an error occurs in step 3, the insert will
not commit, etc. All three steps must succeed, otherwise the transaction
should rollback and notify of an error.
BEGIN
INSERT
if delete file ok then
COMMIT
else
ROLLBACK
end if

may be good enough if you are sufficient sure that the DB transaction
will commit.

But if you really need a single transaction, then you will need:
- a file system that supports XA transactions
- a transaction manager to do 2PC

I would seriously consider the third option of having logic that
checks whether the file has already been inserted before doing
INSERT and then just do your 3 steps after that.

Arne
Oct 30 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
30119
by: Bucfan1 | last post by:
Hello All, I have been encountering trouble with a SQL Server 2000 Transaction log file, mainly with the constant growth and lack of the autoshrink option. Here are the details: 1.) OS is Windows 2000 server sp3 2.) SQL Server 2000 (Build 2195) sp3 3.) Database Recovery Mode is set to Full 4.) Maintenance Plan for "Transaction Log Backup" is set to remove files older then 1hr.
3
5243
by: Daniel Xiao | last post by:
I have set the initial size of the log file for a database to 1M, the maximum size is unrestricted, and the increase rate is 10%. However, when I attempt to delete thousands of rows, the error is still reported that the transaction log file is full. Why can't the log file increase automatically? *** Sent via Devdex http://www.devdex.com *** Don't just participate in USENET...get rewarded for it!
16
12646
by: Michael | last post by:
I have a data application in a2k that I need to create two fixed width text files and then combine them to a single file The first file is header information and the second is transaction data. I have tried and tried but just cant seem to get this right, I am using Queries to created my export files with specifications which works fine, I get stumped with the appending the header to my transaction file. What I have so far looks like...
2
8359
by: alan_sec | last post by:
Can someone tell me what this exception means: Row or object SEKVENCE in EXTLOG001 type *FILE in use Alan
2
2804
by: msuk | last post by:
All, I have a byte array that I save as a file to disk, is there anyway of saving the file as a transaction. For example I save the file to disk and virus scan it, if no virus is found I commit the transaction, if a virus is found I abort the transaction and the file is never written. Is this possible to do with COM+ if not can someone suggest an alternative method. Thanks Msuk
1
2148
by: Hubert Fröhlich | last post by:
Hi list, when doing a large Insert I get in the logfile: 2004-08-03 07:23:46 LOG: recycled transaction log file "000001BA000000BD" 2004-08-03 07:34:00 LOG: recycled transaction log file "000001BA000000C2" 2004-08-03 07:34:00 LOG: recycled transaction log file "000001BA000000C3" 2004-08-03 07:34:00 LOG: recycled transaction log file "000001BA000000C4" 2004-08-03 07:34:00 LOG: recycled transaction log file "000001BA000000C5"...
4
4223
by: Trevor Best | last post by:
I have a client with 150MB database, the transaction log file is nearly 23GB. It's a PITA for me to backup his data and restore it on my server as it takes about 30 minutes to restore as it re-creates a 23GB file with no transactions in it :-\ As well as the space taken up by it, does it do any harm being that size? e.g. does it decrease performance? While it's there, considering the database is backed up daily, does it actually do...
9
1832
by: techking | last post by:
Hello ive written a program but it does not want to read from a referenced text file. i wonder if anyone can tell me what the problem is? below is the source code and after that is the text file. #include<stdio.h> #include<stdlib.h> int main() {
5
3405
by: joeferns79 | last post by:
I have this log ... - infrastructure:ID_UNHANDLED: An un-handled server exception occu rred. Please contact your administrator. at .util.internal.HandleException.getRemoteException(HandleException.java(Compiled Code)) at .util.internal.HandleException.getRemoteException(HandleException.java(Compiled Co nested exception is: infrastructure:RUN_ID_RUNTIME: A runtime exception occurred:...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10350
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10096
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.