Solved

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

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

Technology Partners: 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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 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