ferreira12 Posted February 13, 2014 at 06:12 PM Report #545185 Posted February 13, 2014 at 06:12 PM 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?
nelsonr Posted February 13, 2014 at 11:51 PM Report #545221 Posted February 13, 2014 at 11:51 PM 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
ferreira12 Posted February 14, 2014 at 09:46 AM Author Report #545236 Posted February 14, 2014 at 09:46 AM Devolveu erro na seguinte linha sqlReader = sqlCommand.ExecuteReader() o erro é Execute Reader: A propriedade Connection não foi inicializada.
nelsonr Posted February 14, 2014 at 10:02 AM Report #545239 Posted February 14, 2014 at 10:02 AM (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 February 14, 2014 at 10:02 AM by nelsonr
ribeiro55 Posted February 14, 2014 at 10:02 AM Report #545240 Posted February 14, 2014 at 10:02 AM 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"
ferreira12 Posted February 14, 2014 at 11:42 AM Author Report #545250 Posted February 14, 2014 at 11:42 AM Obrigado, Resultou.
ferreira12 Posted February 27, 2014 at 06:14 PM Author Report #546953 Posted February 27, 2014 at 06:14 PM 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
nelsonr Posted February 27, 2014 at 06:22 PM Report #546955 Posted February 27, 2014 at 06:22 PM Qual o conteúdo da variável AID ? Se for o nome de uma tabela (que é suposto), qual o primeiro campo dessa tabela? Que tipo é ? A tabela tem registos ?
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now