Converting code ADO VBA to ADO net VB.net

G

Gum

I wish to access sql data in a vb net (vs2008) program and have an ADO VBA
(in excel) routine that was used to read the files in excel. The code (in
excel VBA) is:

Sub PriceExtract()
' Create a connection object.
Dim cnLevel As ADODB.Connection
Set cnLevel = New ADODB.Connection
' Provide the connection string.
Dim strConn As String

'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"

'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=mydata;INITIAL
CATALOG=myCat;Trusted_Connection=Yes"

'Now open the connection.
cnLevel.Open strConn
' Create a recordset object.
Dim rsLevel As ADODB.Recordset
Set rsLevel = New ADODB.Recordset

With rsLevel
' Assign the Connection object.
.ActiveConnection = cnLevel
' Extract the required records.
.Open "SELECT * FROM vw_HighPrice"
' Copy the records into cell A1 on Sheet1.
Sheet1.Range("A1").CopyFromRecordset rsLevel

' Tidy up
.Close
.Open "SELECT * FROM vw_MidPrice"
' Copy the records into cell A2 on Sheet1.
Sheet1.Range("A2").CopyFromRecordset rsLevel

' Tidy up
.Close
.Open "SELECT * FROM vw_LowPrice"
' Copy the records into cell A3 on Sheet1.
Sheet1.Range("A3").CopyFromRecordset rsLevel


' Tidy up
.Close
End With

cnLevel.Close
Set rsLevel = Nothing
Set cnLevel = Nothing
End Sub

The code reads lines from the sql view files and excel parses it into say 4
data fields. The code works well in Excel. I wish to access the same data
within the vb net project.

The data in the sql is constantly updated and repeatedly needs to be
accessed by the VB net project.
 
S

Scott M.

You wanted the "Oh, let me rewrite your application for you." response?

If you've been there and done that [correctly], you wouldn't have needed to
post your question.

-Scott


Gum said:
To Mark Rae [MVP]: No Thanks for 'let me google that for you'. Been
there
done that.

I found the answer in one of Bill Vaughn's books.


Mark Rae said:
The code reads lines from the SQL view [files] and Excel parses it into
say 4
data fields. The code works well in Excel. I wish to access the same
data
within the VB.NET project.

http://www.lmgtfy.com/?q="VB.NET"+"ADO.NET"+"SQL+Server"
 

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

Similar Threads


Top