Solved

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

Posted on 2006-07-15
4
189 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
  • 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

13 Experts available now in Live!

Get 1:1 Help Now