Jump to content

Recommended Posts

Posted

Cria passar os resultados de um query para uma label. Estou usar este código mas a única coisa que me devolve é propria query e nao o resultado dela.

O código que estou a utilizar é

Dim cnn As SqlConnection
connetionString = "Data Source=localhost;Initial Catalog=Payroll;User ID=administrator;Passwordxxxxx"
 cnn = New SqlConnection(connection)
 cnn.Open()
 Dim strSQL As String = "select Tipo1 from Tteste where Id='f'"
 lblTeste.Text = strSQL

Onde é que estou a falhar?

Posted

Boas,

falta ai a parte de executar a query e ler o resultado.

Deve ficar algo assim (não testado):

Dim cnn As SqlConnection
cnn = New SqlConnection("Data Source=localhost;Initial Catalog=Payroll;User ID=administrator;Passwordxxxxx")
cnn.Open()

Dim sqlCommand As SqlCommand = New SqlCommand("select Tipo1 from Tteste where Id='f'")
Dim sqlReader As SqlDataReader

sqlReader = sqlCommand.ExecuteReader()
If sqlReader.Read() Then
   lblTeste.Text = sqlReader.GetString(0)
End If
sqlReader.Close()
cnn.Close()

Se o campo Tipo1 não for texto, tens de substituir o GetString pelo tipo correcto

Posted (edited)

Ah ok, falta ai o parametro da connection no sqlcommand

Dim sqlCommand As SqlCommand = New SqlCommand("select Tipo1 from Tteste where Id='f'", cnn)
Edited by nelsonr
Posted

Falta atribuir a connection ao command.

Dim sqlCommand As SqlCommand = New SqlCommand("select Tipo1 from Tteste where Id='f'",cnn)

Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

  • 2 weeks later...
Posted

Bem surgiu me uma nova dúvida em retornar o resultado de uma query

Dim sqlCommand As SqlCommand = New SqlCommand("select * from " + AID, cnn)
 Dim sqlReader As SqlDataReader
 sqlReader = sqlCommand.ExecuteReader()
 If sqlReader.Read() Then
	 AID2 = sqlReader.GetString(0)
	 '
 End If

Queria adicionar a query uma string, e depois da junção me devolve-se o resultado da query na string AID2.

Ele de momento ele está não a retornar nada

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.