2015-11-21 45 views
-1

私人小組btnSave_ClientOrderStatus_Click(BYVAL發件人爲System.Object的,BYVALË作爲System.EventArgs)保存ListView項把手btnSave_ClientOrderStatus.Click mysqlconn =新的MySqlConnection mysqlconn.ConnectionString = serverstring具有多個行和列,以MySQL數據庫

 Try 
     mysqlconn.Open() 
     Dim query As String = "insert into isad.invoice_table(Invoice, Clientnumber, Firstname, Lastname, ProductID, Name, Price, Qty, Description, Total, DateCreated) values " 
     command = New MySqlCommand(query, mysqlconn) 
     For i = 0 To ListView1.Items.Count - 1 
      query &= "('" & txtInvoice_ClientOrderStatus.Text & "','" & txtClientNumber_ClientOrderStatus.Text.Replace("'", "\'") & "','" & txtFname_ClientOrderStatus.Text.Replace("'", "\'") & _ 
       "', '" & txtLname_ClientOrderStatus.Text.Replace("'", "\'") & "', @ProductID" & i.ToString & ", @Name" & i.ToString & ", @Price" & i.ToString & _ 
       ", @Quantity" & i.ToString & ", @Description" & i.ToString & ", '" & txtTotal_ClientOrderStatus.Text & "',now())" 
      command.Parameters.AddWithValue("@ProductID" & i.ToString, ListView1.Items(i).Text) 
      command.Parameters.AddWithValue("@Name" & i.ToString, ListView1.Items(i).SubItems(1).Text) 
      command.Parameters.AddWithValue("@Price" & i.ToString, ListView1.Items(i).SubItems(2).Text) 
      command.Parameters.AddWithValue("@Quantity" & i.ToString, ListView1.Items(i).SubItems(3).Text) 
      command.Parameters.AddWithValue("@Description" & i.ToString, ListView1.Items(i).SubItems(4).Text) 
      query &= ", " 
     Next 
     query = query.Substring(0, query.Length - 2) 

     READER = command.ExecuteReader 
     SBP2.Text = "Status : Client invoice has been created" 
     MsgBox("Saving Client Order Succeed", vbInformation, "Done") 
     mysqlconn.Close() 
     autoincrement_ClientOrderStatus() 
     cleartext_ClientOrderStatus() 
     btnSave_ClientOrderStatus.Enabled = False 
     btnAdd_ClientOrderStatus.Text = "Add Transaction" 
     READER.Close() 
    Catch ex As MySqlException 
     MessageBox.Show(ex.Message) 
     mysqlconn.Close() 
    Finally 
     mysqlconn.Dispose() 

    End Try 
End Sub 

heres my project image, maybe it can help you genospos

Here's the error when i click save

+0

你有問題嗎? – Strawberry

回答

0

如果你要使用DB單一訪問你需要編寫代碼,循環中的所有行組成的查詢字符串:

這應該爲你工作

Dim query As String = "insert into isad.invoice_table(Invoice, Clientnumber, Firstname, Lastname, ProductID, Name, Price, Qty, Description, Total, DateCreated) values " 
Command = New MySqlCommand(query, mysqlconn) 
For i = 0 To ListView1.Items.Count - 1 
    query &= "('" & txtInvoice_ClientOrderStatus.Text & "','" & txtClientNumber_ClientOrderStatus.Text.Replace("'", "\'") & "','" & txtFname_ClientOrderStatus.Text.Replace("'", "\'") & _ 
     "', '" & txtLname_ClientOrderStatus.Text.Replace("'", "\'") & "', @ProductID" & i.ToString & ", @Name" & i.ToString & ", @Price" & i.ToString & _ 
     ", @Quantity" & i.ToString & ", @Description" & i.ToString & ", '" & txtTotal_ClientOrderStatus.Text & "',now())" 
    Command.Parameters.AddWithValue("@ProductID" & i.ToString, ListView1.Items(i).Text) 
    Command.Parameters.AddWithValue("@Name" & i.ToString, ListView1.Items(i).SubItems(1).Text) 
    Command.Parameters.AddWithValue("@Price" & i.ToString, ListView1.Items(i).SubItems(2).Text) 
    Command.Parameters.AddWithValue("@Quantity" & i.ToString, ListView1.Items(i).SubItems(3).Text) 
    Command.Parameters.AddWithValue("@Description" & i.ToString, ListView1.Items(i).SubItems(4).Text) 
    query &= ", " 
Next 
query = query.Substring(0, query.Length - 2) 
+0

@Icebergman_NewProgrammer我編輯了代碼 – genespos

+0

您好先生Genospos,我試圖配置您的代碼,我試着複製所有的文本,但有一個錯誤----「你的SQL語法有錯誤;檢查對應於你的手冊MySQL服務器版本的正確語法使用附近的'「在第1行」..我試圖解決這個問題,但也出現了相同的問題,是否有任何錯誤的代碼?? 順便說一句,感謝回覆 –

+0

@Icebergman_NewProgrammer它似乎在「VALUES」後面有兩個「('」,將「... VALUES('」與「... VALUES」替換爲 – genespos

相關問題