Solved

Open sql database vb.net

Posted on 2014-01-02
4
581 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now