ADO.NET 2.0 - Simple question for you...

V

VB Programmer

I have an ASP.NET 2.0 website. I want to write a function in ADO.NET 2.0
(VB.NET) that does the following:

1. Opens up a connection to my SqlExpress db. (The connectionstring is in
the <connectionStrings> section of my web.config)
2. Runs a query, storing results in a datatable.
3. Closes connection

Can anyone assist?

Thanks!
 
Z

zacks

VB said:
I have an ASP.NET 2.0 website. I want to write a function in ADO.NET 2.0
(VB.NET) that does the following:

1. Opens up a connection to my SqlExpress db. (The connectionstring is in
the <connectionStrings> section of my web.config)
2. Runs a query, storing results in a datatable.
3. Closes connection

Can anyone assist?

Thanks!

Not sure about ASP.NET and SQL Express, but this works for me in VB.NET
and SQL Server:

Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim myDataAdapter As SqlDataAdapter
Dim myDataSet As New DataSet
Dim myDataTable As DataTable

myConnection = New SqlConnection("Data
Source=(local);Database=master;Integrated Security=true;")
myConnection.Open()
myCommand = New SqlCommand("Select blah blah", myConnection)
myDataAdapter = New SqlDataAdapter()
myDataAdapter.SelectCommand = myCommand
myDataAdapter.Fill(myDataSet, "MyTableName")
myDataTable = myDataSet.Tables.Item("MyTableName")
myConnection.Close()
Console.WriteLine("Press any key to continue.")
Console.ReadKey()
 
M

Marina Levit [MVP]

I don't think he/she is having a problem doing it, I think they want to get
the code to do it.

Looks suspiciously like a homework assignment... Not that it should be
difficult to find an example of this online, but whoever this is, seems to
be too lazy to even do a Google search...
 
V

VB Programmer

Thanks Marina, but I'm over 30. Not a homework assignment. :)

I tried Google but apparently wasn't searching for the right stuff...
 
V

VB Programmer

Thanks. I will give it a try zacks.

Not sure about ASP.NET and SQL Express, but this works for me in VB.NET
and SQL Server:

Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim myDataAdapter As SqlDataAdapter
Dim myDataSet As New DataSet
Dim myDataTable As DataTable

myConnection = New SqlConnection("Data
Source=(local);Database=master;Integrated Security=true;")
myConnection.Open()
myCommand = New SqlCommand("Select blah blah", myConnection)
myDataAdapter = New SqlDataAdapter()
myDataAdapter.SelectCommand = myCommand
myDataAdapter.Fill(myDataSet, "MyTableName")
myDataTable = myDataSet.Tables.Item("MyTableName")
myConnection.Close()
Console.WriteLine("Press any key to continue.")
Console.ReadKey()
 
C

Cor Ligthert [MVP]

VB Programmer,

Strange 2 messages before you somebody has asked almost the same question
and got more answers on that including mine with a piece of sample code.

Cor
 
V

VB Programmer

I don't think that was me Cor.

Cor Ligthert said:
VB Programmer,

Strange 2 messages before you somebody has asked almost the same question
and got more answers on that including mine with a piece of sample code.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top