Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

Tags: ado.net

comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

I see a lot of people doing this.

  1. Dim con As New System.Data.SqlClient.SqlConnection
  2.         con.ConnectionString = "..."
  3.         con.Open()
  4.         Dim command As New System.Data.SqlClient.SqlCommand
  5.         command.Connection = con
  6.         command.CommandText = "update tbl set col = 'something'"
  7.         command.ExecuteNonQuery()
  8.         command.Dispose()
  9.         con.Dispose()

I like to use the interfaces instead and use the factory methods that come with it. Something like this.

  1. Dim con As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection
  2.         con.ConnectionString = "..."
  3.         con.Open()
  4.         Dim command As System.Data.IDbCommand = con.CreateCommand
  5.         command.Connection = con
  6.         command.CommandText = "update tbl set col = 'something'"
  7.         command.ExecuteNonQuery()
  8.         command.Dispose()
  9.         con.Dispose()

this way if we ever have to change to an other database we just have to change the = New System.Data.SqlClient.SqlConnection. Of course we would just get a connection from a central place and then we only have to change that little piece of code once.

IDbCommand also has a factory method to create parameters. Since you are using an sqlconnection the factory methods will create sql implementation classes which are optimized to work with MS-SQL-Server.

And even then we have to hope that the databases understand the same dialect ;-)

SO it's not a perfect solution but it can help you on the way to create a more maintainable application.

About the Author

User bio imageChristiaan is a forensic technician who programs on the side, although my function description says that I do IT-things for 90% of the time . I'm an avid VB.NET fan and I use lots of the ALT.Net techniques, like unit-testing, nhibernate, logging, IoC, ...
Social SitingsTwitterLinkedInHomePageLTD RSS Feed
534 views
ado.net, vb.net
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback