Link to home
Start Free TrialLog in
Avatar of OsirisJa
OsirisJa

asked on

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

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
Avatar of nickhoggard
nickhoggard

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
Avatar of OsirisJa

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of nickhoggard
nickhoggard

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so very much!