I am looking for some help and/or advice. My program is written in Visual Basic 2012, and the back-end database is an Access 2007 accdb file.
I have two tables, "Item" and "Item_Attributes". I need to create a record in table "Item" and then create child records in "Item_attribbutes", but I want to use Transactions so I can roll-back if there are any problems during the record creation process. I previously accomplished this by using SQL statements within my VB code.
I am trying to get away from having SQL statements within my code, separate my progaram into tiers, and use DataSets and TableAdapters in the Data Access Layer. I have created the DataSets and TableAdapters so that I can insert, update, and delete records from each table, and they just work fine for single-table operations. I am stuck on figuring out how to wrap those operations into a single transaction. I've read a lot about using "Partial Classes" for my TableAdapters, and I've read about TransactionScope, but I've yet to find a really good example of Visual Basic code that I can follow.
Can anyone provide some advice or point me to a good reference on how to do this? Am I taking the right approach in getting SQL out of my code, or am I just making extra work for myself?
Thanks much!!
My current code, which appears to work just fine, is shown below (edited to remove irrelevant content):
I have two tables, "Item" and "Item_Attributes". I need to create a record in table "Item" and then create child records in "Item_attribbutes", but I want to use Transactions so I can roll-back if there are any problems during the record creation process. I previously accomplished this by using SQL statements within my VB code.
I am trying to get away from having SQL statements within my code, separate my progaram into tiers, and use DataSets and TableAdapters in the Data Access Layer. I have created the DataSets and TableAdapters so that I can insert, update, and delete records from each table, and they just work fine for single-table operations. I am stuck on figuring out how to wrap those operations into a single transaction. I've read a lot about using "Partial Classes" for my TableAdapters, and I've read about TransactionScope, but I've yet to find a really good example of Visual Basic code that I can follow.
Can anyone provide some advice or point me to a good reference on how to do this? Am I taking the right approach in getting SQL out of my code, or am I just making extra work for myself?
Thanks much!!
My current code, which appears to work just fine, is shown below (edited to remove irrelevant content):
Private Function CreateItem() As Boolean
Dim Conn As OleDbConnection
Conn = New OleDbConnection(getConnectionString)
Conn.Open()
Dim cmd As OleDbCommand
' Create the transaction
Dim trans As OleDbTransaction
' Start the transaction
trans = Conn.BeginTransaction(IsolationLevel.ReadCommitted)
Try
ItemID = GetNextItemID()
' ....
' .... code to get data values from data entry form
'...
' Create item record in table Item
strSQL = "INSERT INTO Item " & _
"(ItemID, ProductID, Description, DefaultPurchaseCost, InventoryCost, Inactive, ItemCatalogID) " & _
"VALUES (?, ?, ?, ?, ?, ?, ?)"
cmd = New OleDbCommand(strSQL, Conn, trans)
cmd.Parameters.Add(New OleDbParameter("ItemID", ItemID))
cmd.Parameters.Add(New OleDbParameter("ProductID", ProductID))
cmd.Parameters.Add(New OleDbParameter("Description", Description))
cmd.Parameters.Add(New OleDbParameter("DefaultPurchaseCost", DefaultPurchaseCost))
cmd.Parameters.Add(New OleDbParameter("InventoryCost", DefaultPurchaseCost))
cmd.Parameters.Add(New OleDbParameter("Inactive", Inactive))
cmd.Parameters.Add(New OleDbParameter("ItemCatalogID", ItemCatalogID))
cmd.ExecuteNonQuery()
' Create records in item_attributes table
Dim AttrNum As Integer = 1
strSQL = "INSERT INTO Item_Attribute " & _
"(ItemID, AttributeNumber, AttributeValueID) " & _
"VALUES (?, ?, ?)"
cmd = New OleDbCommand(strSQL, Conn, trans)
cmd.Parameters.Add(New OleDbParameter("ItemID", ItemID))
cmd.Parameters.Add(New OleDbParameter("AttributeNumber", AttrNum))
cmd.Parameters.Add(New OleDbParameter("AttributeValueID", Attr1))
cmd.ExecuteNonQuery()
' Commit all changes
trans.Commit()
Return True
Catch ex As Exception
MsgBox("Items not created", CType((MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation), MsgBoxStyle), "Program Error")
trans.Rollback()
Return False
End Try
Conn.Close()
End Function