Solved

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

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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