Jump to content

[Resolvido] Ajuda Query


hypz

Recommended Posts

Boas, alguém me pode dizer como faço esta query sem dar erro na sintaxe ? Não sei se devo usar apenas "&" para junta-las ou usar mesmo outra função sem ser o "And"

Dim sql As String = "Update Carregamento set Valor_Base=" & valor_base & "And Cod_Cliente=" & cod_cliente & "And Valor_IVA=" & valor_iva & "And Valor_Total=" & valor_total
Edited by Caça
GeSHi
Link to comment
Share on other sites

Repara na concatenação que estás a fazer, no excerto:

... valor_base & "And Cod_Cliente=" & cod_cliente & "And ...

se a variável valor_base for 500 e o cod_cliente 50, o resultador será:

500And Cod_Cliente=50And

O que dará obviamente erro!

Tens de dar uns espaços:

Dim sql As String = "Update Carregamento set Valor_Base=" & valor_base & " And Cod_Cliente=" & cod_cliente & " And Valor_IVA=" & valor_iva & " And Valor_Total=" & valor_total

Link to comment
Share on other sites

Dá-me erro é neste update "Dim sql As String = "Update Utilizador set Montante_Disponivel=" & valor_total & "WHERE Cod_Cartao =" & cod_cliente"

mas eu uso este query noutra form e ele funciona perfeitamente.

Eu aqui estou a inserir dados numa tabela chamada "Carregamento" e depois quero atualizar a tabela "Utilizador" mal ele calcule os valores e insira lá, é possivel ?

Link to comment
Share on other sites

Sim, isso eu sei, pera.

TextBox2.Text = TextBox3.Text * 0.23
	TextBox1.Text = TextBox3.Text - TextBox2.Text
	TextBox2.Visible = True
	TextBox1.Visible = True
	Label1.Visible = True
	Label2.Visible = True
	Dim dr As DialogResult
	dr = MsgBox("Deseja Adicionar ?", MessageBoxButtons.YesNo, "Adicionar Carregamento")
	If dr = DialogResult.Yes Then
		Dim provider As String
		provider = "provider = Microsoft.ACE.OLEDB.12.0;Data Source=Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\hypz\Ambiente de trabalho\pap_teste.accdb; persist security info = false;"
		Dim con As New OleDbConnection(provider)
		con.Open()
		Dim query As String = "INSERT INTO Carregamento([Cod_Recibo], [Cod_Cliente], [Nome_Cliente], [Data_Carregamento], [Valor_Base], [Valor_IVA], [Valor_Total]) VALUES (@Cod_Recibo, @Cod_Cliente, @Nome_Cliente, @Data_Carregamento, @Valor_Base, @Valor_IVA, @Valor_Total)"
		Dim sql As String = "Update Utilizador set Montante_Disponivel=" & valor_total & "WHERE Cod_Cartao =" & cod_cliente
		Dim sqlcom As New OleDbCommand(sql, con)
		Dim nr As Integer
		nr = sqlcom.ExecuteNonQuery()
		Dim command As New OleDbCommand(query, con)
		command.Parameters.Add("@Cod_Recibo", OleDbType.VarChar).Value = TextBox5.Text
		command.Parameters.Add("@Cod_Cliente", OleDbType.VarChar).Value = TextBox4.Text
		command.Parameters.Add("@Nome_Cliente", OleDbType.VarChar).Value = TextBox6.Text
		command.Parameters.Add("@Data_Carregamento", OleDbType.DBDate).Value = DateTimePicker1.MaxDate
		command.Parameters.Add("@Valor_Base", OleDbType.VarChar).Value = TextBox1.Text
		command.Parameters.Add("@Valor_IVA", OleDbType.VarChar).Value = TextBox2.Text
		command.Parameters.Add("@Valor_Total", OleDbType.VarChar).Value = TextBox3.Text
		Dim x As Integer = command.ExecuteNonQuery()
		If x < 1 Then
			MessageBox.Show("Erro ao inserir", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
		Else
			MessageBox.Show("Registo inserido com sucesso!", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
			Dim sql1 As String = "Select * from Carregamento"
			Dim da As OleDbDataAdapter
			da = New OleDbDataAdapter(sql1, con)
			Dim ds As DataSet
			ds = New DataSet()
			da.Fill(ds)
		End If
		con.Close()
		command = Nothing
	ElseIf dr = DialogResult.No Then
		MsgBox("Carregamento Não Adicionado.")
	End If

Dá-me erro no Update do Utilizador.

Edited by Caça
Link to comment
Share on other sites

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.