C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » .NET »

Need details information about this coding


Posted Date: 28 Jun 2009      Posted By: sankar      Member Level: Bronze     Points: 1   Responses: 2



hi to all
this is the function written in app.code. i need that each steps details about this coding and also why this codeing using in app.code ??




Public Function Execute(ByVal strQuery As String) As Integer
Dim recAffected As Integer
Try
If cn.State = ConnectionState.Open Then
cn.Close()
End If
Dim cmd As sqlCommand
cmd = New sqlCommand(strQuery, cn)
cn.ConnectionString = conString
cn.Open()
recAffected = cmd.ExecuteNonQuery()
cn.Close()
Catch ex As Exception

If cn.State = ConnectionState.Open Then
cn.Close()
End If
recAffected = -1
End Try
Return recAffected
End Function

thanks
regards
sankar g





Responses

Author: ABitSmart    28 Jun 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2


Public Function Execute(ByVal strQuery As String) As Integer
Dim recAffected As Integer
Try
If cn.State = ConnectionState.Open Then //is the connection to any DB open ?
cn.Close() //yes, so close it
End If
Dim cmd As sqlCommand
cmd = New sqlCommand(strQuery, cn) //build the command object to execute query
cn.ConnectionString = conString //set the new connection properties
cn.Open() //connect to the DB
recAffected = cmd.ExecuteNonQuery() //execute the query. return the number of rows affected by the query
cn.Close() //close the connection since everything is over now
Catch ex As Exception
//some exception in executing query
If cn.State = ConnectionState.Open Then //is the connection open?
cn.Close() //close it
End If
recAffected = -1 //set the number of records affected as -1 indicating some error occurred.
End Try
Return recAffected //return the total number of records affected by query
End Function



Author: ABitSmart    28 Jun 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

Some optimization to the above code,


Public Function Execute(ByVal strQuery As String) As Integer
Dim recAffected As Integer

Try
Dim cmd As sqlCommand
cmd = New sqlCommand(strQuery, cn) //build the command object to execute query
cn.ConnectionString = conString //set the new connection properties. Not sure if this needed. Is the connection string changed everytime ? If no, then you do not need this line.
cn.Open() //connect to the DB
recAffected = cmd.ExecuteNonQuery() //execute the query. return the number of rows affected by the query

Catch ex As Exception
//some exception in executing query
recAffected = -1 //set the number of records affected as -1 indicating some error occurred.

Finally
//clean up
If cn.State = ConnectionState.Open Then //is the connection open?
cn.Close() //close the connection since everything is over now
End If

End Try

Return recAffected //return the total number of records affected by query
End Function



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : C# project+ setup failed
Previous : Visual studio deployment project+ installer fail to install sql server expr as prerequisit
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use