Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

transaction porblem

Status
Not open for further replies.

tek1ket

MIS
Jan 25, 2009
70
IR
in my code i do serveral function which i realted to eachother, so if any of them fails ,so the rest should be Rollback, so i want to use transaction.any of these function has its own Connection and SQLCOMMAND, so how can i use transaction here , i just write a very simple of the code here

main Part

DeleteRec(id_CT,CoreNo);
InsertRec(id_CT,CoreNo,foudrow);


public void DeleteRec( int Argid_CT,int ArgCoreNo)
{
SqlCommand mySqlCommand=new SqlCommand();
CreateConn();
mySqlCommand= myConn.CreateCommand();
String Str="Delete * from TblCTVknee where id_CT="+Argid_CT + " and CoreNo="+ArgCoreNo;
mySqlCommand.CommandText=Str;
mySqlCommand.ExecuteNonQuery();
mySqlCommand.Dispose();
CloseConnection();
}



public void InsertRec(String ArgStr,String CTArg,DataRow[] ArgRow)
{
SqlCommand mySqlCommand=new SqlCommand();
CreateConn();
mySqlCommand= myConn.CreateCommand();
foreach ( DataRow myRowChild in ArgRow)
{
//Here i do other stuff
}
mySqlCommand.Dispose();
CloseConnection();
}

i want these two function should be related to eachother
, if i put them in one function i can do that, which is not good , because many part of function should be repeated in other functions also,so how should i do that
 
read up on the concept "unit of work". what you have is each function is it's own unit of work. in addition to that your function is responsible for
1. creating a connection
2. creating a command
3. defining the command
4. executing the command
5. closing the command
6. closing the connection

that is too much responsibility for any one object!
by breaking this into a unit of work you have 1 object to create the connection. another to manage opening and closing, possiblity opening a transaction, but this could be a 3rd object. then you have the creation of commands and execution. which could be happening in your functions above.

with web applications this is very simple. request = unit of work = connection. so you can tie into the begin/end request events and create/open/close the connection. a transaction too if needed. then you pass this instance of the connection manager around to your data access code to create commands and execute them.

with smart clients a unit of work is usually equal to the current thread (assuming you're using background threads to execute GUI events.)

there is alot of information about the unit of work pattern on the web. .net examples usually tie in closely with nhibernate or activerecord. there will also be alot of example pertaining to java. the concept is the same.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
i am programming windows applcation , so what subject exctaly should search on the web? ( sorry my english is not so good)

thank you
 
the pattern is still the same "unit of work pattern" the required objects to manage and consume the objects are the same. the only difference is defining the context of the unit of work.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top