Solved

Open sql database vb.net

Posted on 2014-01-02
4
592 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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