Elek Posted May 1, 2008 Report Share Posted May 1, 2008 boas pessoal... é o seguinte, o meu botão guardar estava incorrecto... vou precisar d usar uma instrução em sql para o fazer, porque na query da minha tabela tenho de ter inner join.. exemplo: SELECT Produtos.ID_Produto, Produtos.Designacao, Produtos.Origem, Produtos.PrecoSIva, Produtos.PrecoCIva, Produtos.StockMinimo, Produtos.StockMaximo, Produtos.StockActual, Produtos.PontoEncomenda, Calibre.ID_Calibre, Familia.ID_Familia, Unidade.ID_Unidade FROM Produtos INNER JOIN Calibre ON Produtos.ID_Calibre = Calibre.ID_Calibre INNER JOIN Familia ON Produtos.ID_Familia = Familia.ID_Familia INNER JOIN Unidade ON Produtos.ID_Unidade = Unidade.ID_Unidade isto porque, tenho a tabela calibre, familia e unidade à parte e preciso de as ligar.. o meu problema é que no formulário de inserir novo produto, tenho k escolher por exemplo: familia = frutas, e quero k na tabela dos produtos me apareça que o id da familia é o 3, que vendo na tabela frutas é o id 3, descricao frutas.. bem n sei s m faxo entender 😉 o código que estou a utilizar no botão guardar é este: Dim sql1 As String = "Insert Into Produtos ( Produtos.Designacao, Produtos.Origem, Produtos.PrecoSIva, Produtos.PrecoCIva, Calibre.Descricao) values designacaotextbox.text, origemtextbox.text, precoSIvatextbox.text, precocivatextbox.text, calibretextbox.text" só k isto n ta a funcionar... n dá erro mx tb n funciona lol... eu até meti so alguns parametros pa ver s dava, mx n da... n sei s deveria igualar cada textbox a uma variavel e depois nos values meto a variavel... mas tb n sei cm o fzr... podem m ajudar? Link to comment Share on other sites More sharing options...
Asgorath Posted May 1, 2008 Report Share Posted May 1, 2008 Boas. Assim o insert ja deve dar, se estas ms sql server aconselho o uso de stored procedures. Dim sql1 As String = "Insert Into Produtos ( Produtos.Designacao, Produtos.Origem, Produtos.PrecoSIva, Produtos.PrecoCIva, Calibre.Descricao) values ('" & designacaotextbox.text & "','" & origemtextbox.text & "','" & precoSIvatextbox.text & "','" & precocivatextbox.text & "','" & calibretextbox.text & ")" "The Dark Side Clouds Everthing. Impossible To See The Future Is."My rig: Intel Core 2 Quad Q9450 | abit IP35P | 4G Kingston 800 Mhz | XFX GeForce 9800 GX2 1G ddr3 | 2X WD5000AAJS 500Gb Sata 2 | PSU 600W || Caixa El-Diablo | Creative XMod Link to comment Share on other sites More sharing options...
Elek Posted May 2, 2008 Author Report Share Posted May 2, 2008 eu n m kria tar a meter em stored procuders porque para além d n saber fxr, tenho d entregar o projecto no final deste mês e ta uma beka atrasado... mas parece k vai ter k xer memo axim porque exe código tb n ta a dar =( eu ja tinha conseguido inserir, mas depois tive de fazer a query no inner join nas tabelas e deixei d conseguir novamente :S Link to comment Share on other sites More sharing options...
jpaulino Posted May 2, 2008 Report Share Posted May 2, 2008 Então está resolvido ou não ... fiquei sem saber! Link to comment Share on other sites More sharing options...
Elek Posted May 2, 2008 Author Report Share Posted May 2, 2008 da outra x ficou resolvido com o insert, mas tive de fzr umas query's na bd pkausa das chaves estrangeiras, tive k meter inner joins e isso... e agr o insert ja n funciona... e com este codigo k m deram agr tb n funciona... Link to comment Share on other sites More sharing options...
jpaulino Posted May 2, 2008 Report Share Posted May 2, 2008 Então mostra lá o que tens agora Link to comment Share on other sites More sharing options...
Elek Posted May 2, 2008 Author Report Share Posted May 2, 2008 vou t mostrar a query dentro d uma tabela.. SELECT Produtos.ID_Produto, Produtos.Designacao, Produtos.Origem, Produtos.PrecoSIva, Produtos.PrecoCIva, Produtos.StockMinimo, Produtos.StockMaximo, Produtos.StockActual, Produtos.PontoEncomenda, Produtos.DataValidade, Produtos.QuebrasStock, Calibre.ID_Calibre, Familia.ID_Familia, Unidade.ID_Unidade FROM Produtos INNER JOIN Calibre ON Produtos.ID_Calibre = Calibre.ID_Calibre INNER JOIN Familia ON Produtos.ID_Familia = Familia.ID_Familia INNER JOIN Unidade ON Produtos.ID_Unidade = Unidade.ID_Unidade pronto esta é a query k tenho dentro da tabela produtos... depois tenho um form onde insiro um produto novo de no botão guardar tenho Dim sql1 As String = "Insert Into Produtos ( Produtos.Designacao, Produtos.Origem, Produtos.PrecoSIva, Produtos.PrecoCIva, Calibre.Descricao) values ('" & designacaotextbox.text & "','" & origemtextbox.text & "','" & precoSIvatextbox.text & "','" & precocivatextbox.text & "','" & calibretextbox.text & ")" mas continua sem funcionar... Link to comment Share on other sites More sharing options...
jpaulino Posted May 2, 2008 Report Share Posted May 2, 2008 Experimenta assim: Dim sql1 As String = "Insert Into Produtos ( Produtos.Designacao, Produtos.Origem, Produtos.PrecoSIva, Produtos.PrecoCIva, Calibre.Descricao) values ('" & designacaotextbox.text & "','" & origemtextbox.text & "','" & precoSIvatextbox.text & "','" & precocivatextbox.text & "','" & calibretextbox.text & "')" De qualquer maneira assim é mais difícil de detectar problemas. Porque não usas parameters ? Podes ver aqui um exemplo que utiliza SQL mas mostra como alterar para ODBC: VB.NET: Gestão de Dados em SQL Server - Parte I http://vbtuga.blogspot.com/2008/04/vbnet-gesto-de-dados-em-sql-server.html Link to comment Share on other sites More sharing options...
Elek Posted May 2, 2008 Author Report Share Posted May 2, 2008 vou tentar c os parameters... ja t digo alguma coisa. tks Link to comment Share on other sites More sharing options...
Elek Posted May 2, 2008 Author Report Share Posted May 2, 2008 dá um erro no connection.open () diz assim: Ocorreu um erro ao estabelecer uma ligação ao servidor. Ao ligar ao SQL Server 2005, esta falha poderá ter sido causada por, segundo as predefinições do SQL Server, não serem permitidas ligações remotas. (provider: Fornecedor de Pipes Nomeados, error: 40 - Não foi possível abrir uma ligação ao SQL Server) e pronto... n o sei resolver lol... Link to comment Share on other sites More sharing options...
jpaulino Posted May 2, 2008 Report Share Posted May 2, 2008 Se não mostrares o código ninguem te pode ajudar. Link to comment Share on other sites More sharing options...
Elek Posted May 2, 2008 Author Report Share Posted May 2, 2008 sorry... n mostrei pk era o k tava no teu link... Dim myConnectionString As String = "User ID=sa;password=sa;Initial Catalog=Northwind;Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Programas\MicrosoftSQLServer\MSSQL.1\MSSQL\Data\frutariaa.mdf';" & ";Integrated Security=True;User Instance=True" Dim SQL As String = "INSERT INTO produtos(Produtos.Designacao, Produtos.Origem, Produtos.PrecoSIva, Produtos.PrecoCIva, Calibre.Descricao) VALUES (@Designacao), (@Origem), (@PrecoSIva), (@PrecoCIva), (@Descricao);" Dim connection As New SqlConnection(myConnectionString) Dim command As New SqlCommand(Sql, connection) command.Parameters.Add("@Designacao", SqlDbType.VarChar).Value = " & designacaotextbox.text & " command.Parameters.Add("@Origem", SqlDbType.VarChar).Value = " & origemtextbox.text & " command.Parameters.Add("@PrecoSIva", SqlDbType.Float).Value = " & PrecoSIvaTextBox.text & " command.Parameters.Add("@PrecoCIva", SqlDbType.Float).Value = " & PrecoCIvaTextBox.text & " command.Parameters.Add("@Descricao", SqlDbType.VarChar).Value = " & DescricaoTextBox.text & " connection.Open() Dim x As Integer = command.ExecuteNonQuery() If x < 1 Then MessageBox.Show("A operação efectuada não retomou qualquer resultado.") End If connection.Close() connection = Nothing command = Nothing Link to comment Share on other sites More sharing options...
jpaulino Posted May 2, 2008 Report Share Posted May 2, 2008 Sim mas no meu blog de certeza que não estavam esses error todos 😉 Aqui vai: 1º error: Um ";" a mais Dim myConnectionString As String = "User ID=sa;password=sa;Initial Catalog=Northwind;Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Programas\MicrosoftSQLServer\MSSQL.1\MSSQL\Data\frutariaa.mdf';Integrated Security=True;User Instance=True" 2º erro: O insert statement é direrente. É "INSERT INTO tabela (campos) VALUES (valores);" Dim SQL As String = "INSERT INTO produtos(Produtos.Designacao, Produtos.Origem, Produtos.PrecoSIva, Produtos.PrecoCIva, Calibre.Descricao) VALUES (@Designacao, @Origem, @PrecoSIva, @PrecoCIva, @Descricao);" 3º erro: Os parametros são passados assim command.Parameters.Add("@Designacao", SqlDbType.VarChar).Value = designacaotextbox.text Tenta e diz qq coisa. Link to comment Share on other sites More sharing options...
Elek Posted May 2, 2008 Author Report Share Posted May 2, 2008 é mesmo insert intro? Link to comment Share on other sites More sharing options...
jpaulino Posted May 2, 2008 Report Share Posted May 2, 2008 Sorry! Insert into Link to comment Share on other sites More sharing options...
Elek Posted May 2, 2008 Author Report Share Posted May 2, 2008 n consigo entender od ta o 2º erro desculpa... Link to comment Share on other sites More sharing options...
jpaulino Posted May 2, 2008 Report Share Posted May 2, 2008 Então tu tens isto: Dim SQL As String = "INSERT INTO produtos(Produtos.Designacao, Produtos.Origem, Produtos.PrecoSIva, Produtos.PrecoCIva, Calibre.Descricao) VALUES (@Designacao), (@Origem), (@PrecoSIva), (@PrecoCIva), (@Descricao);" E é isto: Dim SQL As String = "INSERT INTO produtos(Produtos.Designacao, Produtos.Origem, Produtos.PrecoSIva, Produtos.PrecoCIva, Calibre.Descricao) VALUES (@Designacao, @Origem, @PrecoSIva, @PrecoCIva, @Descricao);" Ou seja abreviado tens isto: “INSERT INTO Tabela (campo1, campo2) VALUES (valor1), (valor2)” E é: “INSERT INTO Tabela (campo1, campo2) VALUES (valor1, valor2)” Deu para entender ? Link to comment Share on other sites More sharing options...
Elek Posted May 2, 2008 Author Report Share Posted May 2, 2008 LOOOOL k estupida... eu copiei um pa cima do outro e td e n entendia memo o erro lol.. desculpa a sério, agr ja entendi.. isto é o canxaxo lol ? Link to comment Share on other sites More sharing options...
Elek Posted May 2, 2008 Author Report Share Posted May 2, 2008 bem sem kerer ser xata lol continua sem dar... tava a tentar outro código mas tb da erro... o código é: Private Function valida() As Boolean 'verifica se algumas informações foram fornecidas If DesignacaoTextBox.Text = String.Empty Or OrigemTextBox.Text = String.Empty Or FamiliaComboBox.Text = String.Empty Or CalibreComboBox.Text = String.Empty Or PrecoSIvaTextBox.Text = String.Empty Or IvaComboBox.Text = String.Empty Or UnidadeComboBox.Text = String.Empty Or PrecoCIvaTextBox.Text = String.Empty Then Return False Else Return True End If End Function (dps no código do botao guardar) Dim sql As String = "" Dim strConn As String = My.Settings.frutariaaConnectionString DesignacaoTextBox.Focus() If valida() = True Then sql = "INSERT INTO Clientes (Designacao, Origem, ID_Calibre, ID_Familia, PrecoSIva, PrecoCIva, ID_Unidade, StockMinimo, StockMaximo, StockActual, PontoEncomenda, DataValidade) VALUES('" & DesignacaoTextBox.Text & "' ,'" & OrigemTextBox.Text & "', '" & CalibreComboBox.Text & "','" & FamiliaComboBox.Text & "', '" & PrecoSIvaTextBox.Text & "', '" & PrecoCIvaTextBox.Text & "', '" & UnidadeComboBox.Text & "', '" & StockMinimoNumUD.Value & "', '" & StockMaximoNumUD.Value & "','" & StockActualNumUD.Value & "', '" & PontoEncomendaNumUD.Value & "', '" & DataValidadeDTPicker.Text & "')" Dim con As New SqlConnection(strConn) Dim cmd As New SqlCommand(sql, con) Try con.Open() cmd.ExecuteNonQuery() MsgBox("Produto inserido com sucesso!") Catch ex As Exception MsgBox(ex.Message) End Try End If End Sub O erro é: uma msgbox k diz invalid column name 'Designacao', invalid column name 'Origem', invalid column name 'ID_Calibre', invalid column name 'ID_Familia', invalid column name 'PrecoSIva', invalid column name 'PrecoCIva', invalid column name 'ID_Unidade', invalid column name 'StockMinimo', invalid column name 'StockMaximo', invalid column name 'StockActual', invalid column name 'PontoEncomenda', invalid column name 'DataValidade'. desculpa o incómodo, a sério... mas eu tou à beira do desespero lool qual axas k é o erro mais facil d corrigir? é k o outro continua m a dar erro no connection.open() 😉 Link to comment Share on other sites More sharing options...
jpaulino Posted May 2, 2008 Report Share Posted May 2, 2008 Mas olha lá à pouco a tabela era produtos agora é clientes ? Link to comment Share on other sites More sharing options...
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