Results 1 to 11 of 11

Thread: [RESOLVED] Which queries is looks better?

  1. #1

    Thread Starter
    Hyperactive Member AceDuk's Avatar
    Join Date
    Jan 2016
    Location
    Macedonia
    Posts
    465

    Resolved [RESOLVED] Which queries is looks better?

    1.
    vb.net Code:
    1. 'Insert
    2.             Try
    3.                 Dim strSql As String = "INSERT INTO Users ([Username], [Password]) VALUES (@Username,@Password)"
    4.                 con.Open()
    5.                 Using cmd As New OleDbCommand(strSql, con)
    6.                     cmd.Parameters.AddWithValue("@Username", txtUsername.Text)
    7.                     cmd.Parameters.AddWithValue("@Password", txtPassword.Text)
    8.                     cmd.ExecuteNonQuery()
    9.                 End Using
    10.                 con.Close()
    11.                 MsgBox("Successfully saved", vbInformation, "")
    12.             Catch ex As Exception
    13.                 MsgBox(ex.Message)
    14.                 con.Close()
    15.             End Try
    16.  
    17. 'Updating
    18.             Try
    19.                 con.Open()
    20.                 Dim strSql As String = "UPDATE Users SET [Username]= @Username, [Password]= @Password WHERE ID = @ID"
    21.                 Using cmd As New OleDbCommand(strSql, con)
    22.                     cmd.Parameters.AddWithValue("@Username", txtUsername.Text)
    23.                     cmd.Parameters.AddWithValue("@Password", txtPassword.Text)
    24.                     cmd.Parameters.AddWithValue("@ID", labelID.Text)
    25.                     cmd.ExecuteNonQuery()
    26.                 End Using
    27.                 con.Close()
    28.                 MsgBox("Successfully saved", vbInformation, "")
    29.             Catch ex As Exception
    30.                 MsgBox(ex.Message)
    31.                 con.Close()
    32.             End Try
    33.  
    34.   'Removing
    35.             Try
    36.                 Dim strSql As String = "DELETE from Users WHERE ID = @ID"
    37.                 con.Open()
    38.                 Using cmd As New OleDbCommand(strSql, con)
    39.                     cmd.Parameters.AddWithValue("@ID", labelID.Text)
    40.                     cmd.ExecuteNonQuery()
    41.                 End Using
    42.                 con.Close()
    43.                 MsgBox("Successfully removed", vbInformation, "")
    44.             Catch ex As Exception
    45.                 MsgBox(ex.Message)
    46.                 con.Close()
    47.             End Try
    2.
    vb.net Code:
    1. 'Insert
    2.             Try
    3.                 con.Open()
    4.                 Dim insert_user As OleDbCommand = New OleDbCommand("INSERT INTO Users ([Username], [Password]) VALUES (@Username,@Password)", con)
    5.                 insert_user.Parameters.AddWithValue("@Username", txtUsername.Text)
    6.                 insert_user.Parameters.AddWithValue("@Password", txtPassword.Text)
    7.                 insert_user.ExecuteNonQuery()
    8.                 insert_user.Dispose()
    9.                 con.Close()
    10.                 MsgBox("Successfully saved", vbInformation, "")
    11.             Catch ex As Exception
    12.                 MsgBox(ex.Message)
    13.                 con.Close()
    14.             End Try
    15.  
    16. 'Updating
    17.             Try
    18.                 con.Open()
    19.                 Dim update_user As New Data.OleDb.OleDbCommand("UPDATE Users SET [Username]= @Username, [Password]= @Password WHERE ID = @ID;", con)
    20.                 update_user.Parameters.AddWithValue("@Username", txtUsername.Text)
    21.                 update_user.Parameters.AddWithValue("@Password", txtPassword.Text)
    22.                 update_user.Parameters.AddWithValue("@ID", labelID.Text)
    23.                 update_user.ExecuteNonQuery()
    24.                 update_user.Dispose()
    25.                 con.Close()
    26.                 MsgBox("Successfully saved", vbInformation, "")
    27.             Catch ex As Exception
    28.                 MsgBox(ex.Message)
    29.                 con.Close()
    30.             End Try
    31.  
    32.  'Removing
    33.             Try
    34.                 con.Open()
    35.                 Dim delete As New OleDbCommand("DELETE from Users WHERE ID = @ID;", con)
    36.                 delete.Parameters.AddWithValue("@ID", labelID.Text)
    37.                 delete.ExecuteNonQuery()
    38.                 con.Close()
    39.                 MsgBox("Successfully removed", vbInformation, "")
    40.             Catch ex As Exception
    41.                 MsgBox(ex.Message)
    42.                 con.Close()
    43.             End Try
    Thanks

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Which queries is looks better?

    The former. However, I'd use the Using statement for the Connection object, not just the Command object. After all, if con.Open throws an exception, are you sure that con.Close even makes sense? The Using construct will take care of closing and cleaning up the connection, whether an exception is thrown or not, so you don't have to worry about it.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Hyperactive Member AceDuk's Avatar
    Join Date
    Jan 2016
    Location
    Macedonia
    Posts
    465

    Re: Which queries is looks better?

    how i understand you say me to remove con.open, con.close?
    If i remove that lines of code i always get errors

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Which queries is looks better?

    You have to have con.open, anyways. What I was saying was something like this:
    Code:
    Using con As New OleDB.OleDBConnection(yourConnectionStringHere)
     Try
                    Dim strSql As String = "INSERT INTO Users ([Username], [Password]) VALUES (@Username,@Password)"
                    con.Open()
                    Using cmd As New OleDbCommand(strSql, con)
                        cmd.Parameters.AddWithValue("@Username", txtUsername.Text)
                        cmd.Parameters.AddWithValue("@Password", txtPassword.Text)
                        cmd.ExecuteNonQuery()
                    End Using
                    
                    MsgBox("Successfully saved", vbInformation, "")
                Catch ex As Exception
                    MsgBox(ex.Message)
                    
                End Try
       End Using
    So, the Open is still there, but the Close has been removed, because EndUsing will deal with that correctly, whether an exception is thrown, or not.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Hyperactive Member AceDuk's Avatar
    Join Date
    Jan 2016
    Location
    Macedonia
    Posts
    465

    Re: Which queries is looks better?

    I forgot to say that i always use connection string in module
    vb.net Code:
    1. Imports System.Data.OleDb
    2. Module Module1
    3.     Public con As New OleDbConnection("Provider=MICROSOFT.ACE.OLEDB.12.0; Data Source=|DataDirectory|/database.accdb")
    4. End Module
    Please Mark your Thread "Resolved", if the problem is solved & Rate those who have helped you

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Which queries is looks better?

    Ah, in that case, the Using wouldn't work right, but then again, there's no good reason to handle a connection that way. You may think that sharing a connection around is fine, and it probably is as long as you need only one, but connections are a limited resource. As a general rule, get one, use it, get rid of it, all as fast as you can. With an Access DB, it often doesn't matter much, because Access doesn't deal well with multiple concurrent users, so it tends to do best with just a single user, and that's how it is generally used. Once you have multiple concurrent users, then creating and disposing of the connection as I showed with the Using statement will work better.

    You may think that it's wasteful to create new connections all the time like that, and you'd have been right at one point (I don't know how old you are), but not in .NET, not any longer. Even when you dispose of a connection (like the End Using will do), it doesn't really go away. It is just released back into a connection pool. When you create a connection, if there is one available in the pool, you get that one. Only if there are none available in the pool will a new one be created.

    For example, I have a program which may create then dispose of, a couple dozen connections during startup. However, if I look at SQL Server, I see that there are only three or four actual connections that have been created. Those three or four have just been re-used over and over by the different parts of the program. It's all pretty efficient.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Hyperactive Member AceDuk's Avatar
    Join Date
    Jan 2016
    Location
    Macedonia
    Posts
    465

    Re: Which queries is looks better?

    so you suggest me to get rid of Module, and how you say: get one, use it, get rid of it, all as fast as you can.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Which queries is looks better?

    I've done it both ways. The way you have it is perfectly fine if there will only be one program connecting to the database at a time, which is often the case. If there could be multiple users, then changing it would be advisable.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Hyperactive Member AceDuk's Avatar
    Join Date
    Jan 2016
    Location
    Macedonia
    Posts
    465

    Re: Which queries is looks better?

    so if one program use that database it's better my suggestion (With Module connection)
    also your suggest is look good if i delete module - maybe i should use your suggestion because i avoid con.close

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Which queries is looks better?

    There is that benefit.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Hyperactive Member AceDuk's Avatar
    Join Date
    Jan 2016
    Location
    Macedonia
    Posts
    465

    Re: Which queries is looks better?

    Ok thanks for help

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