Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with getting info from a SQL Server 2005 database in VB.NET 2005

Posted on 2006-07-15
4
Medium Priority
?
198 Views
Last Modified: 2010-04-23
I am using SQL Server to store information in my program. How can I access the database and load up some variables into variables?

I have this section of the code already:

Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Payroll.mdf;Integrated Security=True;User Instance=True")

Thank you very much in advance
0
Comment
Question by:OsirisJa
[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 5

Expert Comment

by:nickhoggard
ID: 17116703
Hi,

You can get your data into a DataTable using the following

       Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Payroll.mdf;Integrated Security=True;User Instance=True")
        Dim objDataAdapter As New SqlDataAdapter("SELECT * FROM MY_TABLE", conn)
        Dim objDataTable As New DataTable
        objDataAdapter.Fill(objDataTable)

You could then use the DataTable however you need to within your application.  For example you could use it as the datasource on a DataGridView:

dataGridView1.DataSource = objDataTable

Hope this helps get you started.

Cheers

Nick
0
 

Author Comment

by:OsirisJa
ID: 17116708
How do I get information from teh database loaded into variables

eg

var1 = FirstName
var2 = LastName

with firstname and lastname being informatyion in the database
0
 
LVL 5

Accepted Solution

by:
nickhoggard earned 2000 total points
ID: 17116811
Hi,

I am assuming you are only trying to retrieve one single record since you are trying to get it into variables rather than a table.

You can either read the data from the first row of the data table as below (note you'll have to change the sql statement based on your own table structure etc):

       Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Payroll.mdf;Integrated Security=True;User Instance=True")
        Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Payroll.mdf;Integrated Security=True;User Instance=True")
        Dim objDataAdapter As New SqlDataAdapter("SELECT FIRST_NAME, LAST_NAME FROM USERS WHERE ID = 1", conn)
        Dim objDataTable As New DataTable
        objDataAdapter.Fill(objDataTable)

        Dim strFirstName As String = objDataTable.Rows(0)("FIRST_NAME")
        Dim strLastName As String = objDataTable.Rows(0)("LAST_NAME")
 
------------------
Or you could use a SqlCommand and SqlDataReader to get the data:

        Dim strFirstName As String
        Dim strLastName As String

       Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Payroll.mdf;Integrated Security=True;User Instance=True")
        Dim objCommand As SqlCommand = conn.CreateCommand
        objCommand.CommandText = "SELECT FIRST_NAME, LAST_NAME FROM USERS WHERE ID = 1"
        Dim objReader As SqlDataReader = objCommand.ExecuteReader
        If objReader.Read Then
            strFirstName = objReader("FIRST_NAME")
            strLastName = objReader("LAST_NAME")
        End If
        objReader.Close()
       
Cheers

Nick
0
 

Author Comment

by:OsirisJa
ID: 17119701
Thank you so very much!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

730 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