Results 1 to 7 of 7

Thread: [RESOLVED] [2005] Connection

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [RESOLVED] [2005] Connection

    Hi all
    If we are using more then one table in my code for saving the data. If both table contain different data then what I want
    I want to delete data from first table, if I get error when I am saving then data in the second table.
    any property we have directly

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Connection

    You need to wrap both operations in a transaction, so either everything completes or nothing does. What database are you using and are you using TableAdapters or DataAdapters?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] Connection

    Presently I am using the Access but I want this things on the SQL Server also,I am using the datatable.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Connection

    If you want to use SQL Server then you should use SQL Server because you'll have to change your code to switch. You can use MSDE or SQL Server Express to develop even if you intend to use a full version of SQL Server when you deploy. If you want to code transactions then it will be different in each case so you should make up your mind now.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] Connection

    Presently I am using the access database , so you guide me about the access.
    Thanks

  6. #6

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Thumbs up Re: [2005] Connection

    Finally Got the solution
    While a transaction is in progress parts of the database will get locked out
    from other processes that are trying to access it. This is to ensure the safety of the transaction and other processes that are accessing the database. When you start a transaction perform the queries you need quickly so that the locks don't last long. And do not wait for user input inside a transaction.

    VB Code:
    1. myConnection.BeginTransaction()
    2. 'do stuff with the database here
    3. If someErrorHappened Then
    4.    myConnection.RollbackTransaction()  
    5.   Return
    6. End If
    7. myConnection.CommitTransaction()

  7. #7

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [RESOLVED] [2005] Connection

    This is the advance function for connection transaction,commit and the rollback!!
    While a transaction is in progress parts of the database will get locked out
    from other processes that are trying to access it. This is to ensure the safety of the transaction and other processes that are accessing the database. When you start a transaction perform the queries you need quickly so that the locks don't last long. And do not wait for user input inside a transaction.

    tell me if any suggestion!!



    VB.NET Code:
    1. #Region "Advance Function for connection rollback And Transaction By Shakti"
    2.     ':Author ::::Shakti Singh Dulawat
    3.     Public Function ExecuteMultipleQuery(ByVal QueryString() As String) As Boolean
    4.         Dim myConnection As SqlClient.SqlConnection = Nothing
    5.         Dim transaction As System.Data.SqlClient.SqlTransaction
    6.         Dim command As DbCommand
    7.         myConnection = OpenNewConnection() 'Open The SQL connection Here
    8.         command = myConnection.CreateCommand()
    9.         Try
    10.             transaction = myConnection.BeginTransaction()
    11.             command.CommandType = CommandType.Text
    12.             command.Transaction = transaction
    13.             command.Connection = myConnection
    14.             For Each query As String In QueryString
    15.                 command.CommandText = query
    16.                 command.ExecuteNonQuery()
    17.             Next
    18.             transaction.Commit()
    19.             Return True
    20.         Catch ex As SqlClient.SqlException
    21.             Throw New Exception("SQL Exception ", ex)
    22.             transaction.Rollback()
    23.             Return False
    24.         Catch exx As Exception
    25.             Throw New Exception("Other ", exx)
    26.             transaction.Rollback()
    27.             Return False
    28.         Finally
    29.             myConnection.Close()
    30.             command.Dispose()
    31.         End Try
    32.     End Function
    33. #End Region

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width