Solved

Open sql database vb.net

Posted on 2014-01-02
4
590 Views
Last Modified: 2014-01-02
I'm trying to get a grasp of how to perform basic database functions within VB.Net.  Can someone please provide a simple example of how to:
  Open a SQL database
  Perform a simple "SELECT *" query
  Loop through the result set one record at a time
  Perform a conditional on a datafield  (e.g.: IF "datafield" = 1 THEN)
  Close the database

Generic code examples would work fine.

Also, which project references would I need to include in my VB.Net app.

Thanks.
0
Comment
Question by:TheChos
  • 2
  • 2
4 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39751267
In very simple terms you would be looking at something like the following:
Dim cn As New SqlConnection("Your connection string")
Dim cmd As New SqlCommand("SELECT Something FROM SomeTable", cn)

cn.Open()

Dim dr As New SqlDataReader = cmd.ExecuteReader()

While dr.Read()

     If dr.GetInteger(0) = 1 Then
         '// do something
     End If

End While

cn.Close()

Open in new window

Note: Written from memory so may contain syntax errors :)

P.S. This uses the System.Data.SqlClient namespace and the System.Data assembly (which should be referenced in the project by default)
0
 

Author Comment

by:TheChos
ID: 39751339
All works great except that I'm having a little difficulty accessing the fields by name.  Could you send me an example of how to retrieve the value of a field named "ExampleField"?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39751342
The DataReader exposes a method called GetOrdinal() which return the index of a column from its name. You can build that into your code to get fields by name:
     If dr.GetInt32(dr.GetOrdinal("SomeColumnName")) = 1 Then
         '// do something
     End If

Open in new window

0
 

Author Closing Comment

by:TheChos
ID: 39751385
Worked Great!  Thank you very much.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question