Solved

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

Posted on 2006-07-15
4
196 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 500 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

623 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