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.
(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.