2017-05-02 28 views
0

我在VB中創建一個Windows窗體應用程序,它將統計datagridview控件中的行並將它們插入到SQL數據庫中。問題在於,每次單擊按鈕時,datagridview中的行數都不相同。我想從一個datagridview添加一個可變數量的項目到一個SQL數據庫

我需要發生的是每個datagridview行都會將項目名稱和項目數量插入到數據庫中。

下面是代碼:

Private Sub Button22_Click(sender As Object, e As EventArgs) Handles Button22.Click 
    Dim myconnect As New SqlConnection 
    myconnect.ConnectionString = "Data Source=SEA-1800033547\SQLEXPRESS;database=operations; 


    Dim mycommand As SqlClient.SqlCommand = New SqlClient.SqlCommand() 
    mycommand.Connection = myconnect 


    mycommand.CommandText = "INSERT INTO odrcmp (CustName, CustAddress,Phone,email,item1,item1qty,item2,item2qty,date) VALUES (@custname, @custaddress, @phone, @email,@item1,@item1qty,@item2,@item2qty,@date)" 
    myconnect.Open() 

    Try 

     mycommand.Parameters.Add("@custname", SqlDbType.NChar).Value = customername.Text 
     mycommand.Parameters.Add("@custaddress", SqlDbType.NChar).Value = customeraddress.Text 
     mycommand.Parameters.Add("@phone", SqlDbType.NChar).Value = phonenumber.Text 
     mycommand.Parameters.Add("@email", SqlDbType.NChar).Value = email.Text 
     mycommand.Parameters.Add("@date", SqlDbType.NChar).Value = Label1.Text 



     For i As Integer = 1 To DataGridView1.Rows.Count - 1 
      For Each row In DataGridView1.Rows 
       mycommand.Parameters.Add("@item" & i, SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(0).Value 
       mycommand.Parameters.Add("@item" & i & "qty", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(2).Value 
       mycommand.Parameters.Clear() 
      Next 
     Next 

     mycommand.ExecuteNonQuery() 
     MsgBox("Success") 
    Catch ex As System.Data.SqlClient.SqlException 
     MsgBox(ex.Message) 
    End Try 
    myconnect.Close() 




End Sub 

當我沒有在代碼中mycommand.Parameters.Clear()行,它會說,還在尋找標值ITEM2,這導致我相信在某個地方循環被搞砸了。

當我添加mycommand.Parameters.Clear()它表示它正在尋找@custname。我相信那是因爲代碼循環通過。

+0

我我剛剛刪除了我原來的答案,因爲我認爲這是不對的。什麼是嵌套循環的原因?你有'For i'循環,然後是'For each'循環。 –

+0

首先,循環中的這一行「mycommand.Parameters.Clear()」將刪除所有參數。這就是爲什麼它說它需要@custname。其次,你爲什麼使用嵌套? – Han

+0

@SteveLovell我做了嵌套循環試圖讓它工作。當我使用「for i」循環時,它會錯誤地聲明「必須聲明item1標量變量。通過」For Each Row「循環,我無法弄清楚如何讓變量與item一起工作。 「item」&row「它說:System.InvalidCastException:'操作符'&'沒有爲字符串」item「定義並鍵入'DataGridViewRow'。'。我很感激你在這方面與我一起工作! – Rich

回答

0

我沒有一個簡單的方法來測試這個,但以下是你的循環的重做版本。下半部分,試圖設置任何未使用的項目字段的參數可能不需要,因爲我相信他們應該在任何情況下默認爲NULL。如果不需要unusedRows循環,則可以刪除rowNo變量並使用行索引。

Dim rowNo as Integer 
rowNo = 1 
For Each row In DataGridView1.Rows 
    If Not row.IsNewRow Then 
     mycommand.Parameters.Add("@item" & rowNo, SqlDbType.NChar).Value = Row.Cells(0).Value.ToString 
     mycommand.Parameters.Add("@item" & rowNo & "qty", SqlDbType.NChar).Value = Row.Cells(2).Value.ToString 
     rowNo = rowNo + 1 
    End if 
Next row 

'Possible you don't need the remainder, I think the unset params should default to this anyway. 

Dim unusedRowsNo as Integer 
Dim maxRows as Integer 

'Change the following to how many rows you want to handle in the DataGrid. 
maxRows = 10 

For unusedRowNo = rowNo to maxRows 
    mycommand.Parameters.Add("@item" & unusedRowNo, SqlDbType.NChar).Value = DBNull.Value 
    mycommand.Parameters.Add("@item" & unusedRowNo & "qty", SqlDbType.NChar).Value = DBNull.Value 
Next unusedRowNo 
+0

謝謝!當我今晚下班回家時,我會試試看。 – Rich

+0

該代碼與Raffaello的上述代碼具有相同的功能。它繼續查找item3。 – Rich

+0

這絕對有可能。這是緩慢的進展,因爲我們沒有坐在一起。也因爲我是一個非常業餘的.net開發人員。您的'CommandText'是否與分配給'maxRows'的值一致? –

0

現在我明白你的程序是如何工作的;

這裏是我的解決方案

記得設置DataGridView1.AllowUserToAddRow =假(否則For循環會考慮也是DataGridView1的最後一行(添加新行))

Dim CNN As New SqlClient.SqlConnection 

    Try 

     CNN.ConnectionString = "Data Source=SEA-1800033547\SQLEXPRESS;database=operations" 

     CNN.Open() 

     Dim cmdCommand As SqlClient.SqlCommand = CNN.CreateCommand 
     cmdCommand.CommandText = "INSERT INTO ordcmp (CustName, CustAddress, Phone, email, item1, item1qty, item2, item2qty, date) VALUES (@CustName, @CustAddress, @Phone, @email, @item1, @item1qty, @item2, @item2qty, @date)" 

     cmdCommand.Parameters.Add("@CustName", SqlDbType.VarChar, 50).Value = customerName.Text 
     cmdCommand.Parameters.Add("@CustAddress", SqlDbType.VarChar, 50).Value = customerAddress.Text 
     cmdCommand.Parameters.Add("@Phone", SqlDbType.VarChar, 50).Value = phonenumber.Text 
     cmdCommand.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = email.Text 
     cmdCommand.Parameters.Add("@date", SqlDbType.DateTime2, 20).Value = Label1.Text 


     For iRow As Integer = 0 To DataGridView1.RowCount - 1 

      Dim DGVR As DataGridViewRow = DataGridView1.Rows(iRow) 

      cmdCommand.Parameters.Add("@item" & (iRow + 1), SqlDbType.VarChar, 50).Value = DGVR.Cells(1).Value 
      cmdCommand.Parameters.Add("@item" & (iRow + 1) & "qty", SqlDbType.Int, 9).Value = DGVR.Cells(2).Value 

     Next 

     cmdCommand.ExecuteNonQuery() 

     CNN.Close() 
     CNN.Dispose() 
     MessageBox.Show("Success") 


    Catch ex As SqlClient.SqlException 
     CNN.Close() 
     CNN.Dispose() 
     MessageBox.Show(ex.ToString) 


    Catch ex As Exception 
     MessageBox.Show(ex.ToString) 

    End Try 
+0

感謝您在嘗試中打開連接,catch塊。我認爲我沒有像我需要的那樣清楚,我很抱歉。要回答你的問題,datagridview中的列是0(對於項目名稱)和2(對於數量)。我很欣賞代碼,但我最終會有超過2個項目(我認爲限制將會是10個項目)。假設我的客戶購買了1個布朗尼蛋糕和1個餅乾訂單。這將在datagridview中顯示爲兩個不同的行。我想將布朗尼放入item1中,並將數量作爲item1qty。餅乾將是item2等,等等。 – Rich

+0

我編輯了我以前的答案;現在我認爲它正在按照你的要求工作 – Raffaello

+0

謝謝!當我今晚下班回家時,我會放棄它。 – Rich

相關問題