| | |
Problem with connection to SQL database
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 32
Reputation:
Solved Threads: 0
Hi,
I have problem when trying to connect to databse and execute query.
Here is the full code:
Private Sub OpenDataReader()
Dim strConn As String = "Server=localhost;Database=Northwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(strConn)
cnnNwind.Open()
Dim strSQL As String = "SELECT * FROM Shippers"
strSQL += "; SELECT EmployeeID,FirstName,LastName FROM Employees"
Dim cmdReader As SqlCommand = New SqlCommand(strSQL, cnnNwind)
cmdReader.CommandText = CommandType.Text
Dim sdrReader As SqlDataReader = cmdReader.ExecuteReader(CommandBehavior.CloseConnection)
'sdrReader = cmdReader.ExecuteReader
With sdrReader
If .HasRows Then
While .Read
lstShippers.Items.Add(.Item(0).ToString + " - " + .Item(1).ToString)
End While
While .NextResult
While .Read
lstEmployees.Items.Add(.Item(0).ToString + " - " + .Item(1).ToString + " " + .Item(2).ToString)
End While
End While
End If
.Close()
End With
End Sub
I get the message "Incorrect syntax near '1' " pointing to line where sdrReader is introduced.
I have problem when trying to connect to databse and execute query.
Here is the full code:
Private Sub OpenDataReader()
Dim strConn As String = "Server=localhost;Database=Northwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(strConn)
cnnNwind.Open()
Dim strSQL As String = "SELECT * FROM Shippers"
strSQL += "; SELECT EmployeeID,FirstName,LastName FROM Employees"
Dim cmdReader As SqlCommand = New SqlCommand(strSQL, cnnNwind)
cmdReader.CommandText = CommandType.Text
Dim sdrReader As SqlDataReader = cmdReader.ExecuteReader(CommandBehavior.CloseConnection)
'sdrReader = cmdReader.ExecuteReader
With sdrReader
If .HasRows Then
While .Read
lstShippers.Items.Add(.Item(0).ToString + " - " + .Item(1).ToString)
End While
While .NextResult
While .Read
lstEmployees.Items.Add(.Item(0).ToString + " - " + .Item(1).ToString + " " + .Item(2).ToString)
End While
End While
End If
.Close()
End With
End Sub
I get the message "Incorrect syntax near '1' " pointing to line where sdrReader is introduced.
Your trouble is not trouble accessing the database, it seems that is working just fine.
See MSDN
http://209.85.165.104/search?q=cache...lnk&cd=3&gl=us
See MSDN
http://209.85.165.104/search?q=cache...lnk&cd=3&gl=us
--
"Dummy."
"Dummy."
![]() |
Other Threads in the VB.NET Forum
- Previous Thread: Label not changing
- Next Thread: how to link microsoft access field with vb.net
| Thread Tools | Search this Thread |
"crystal .net .net2005 30minutes 2005 2008 access account arithmetic array assignment basic binary bing button buttons center check code combobox component connectionstring convert crystalreport data database databasesearch datagrid datagridview design dissertation dissertations dissertationthesis dosconsolevb.net dropdownlist excel file-dialog firewall folder ftp google hardcopy image images insert isnumericfuntioncall listview login math memory mobile ms navigate net networking opacity output passingparameters peertopeervideostreaming picturebox picturebox1 port print problemwithinstallation project reports" save savedialog searchbox serial soap sorting string table tcp temp text textbox timer toolbox trim update updown upload useraccounts usercontrol vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet view visual visualbasic visualbasic.net visualstudio web wpf





