插入在数据库中添加额外的行

时间:2011-11-09 11:00:56

标签: vb.net ado.net

当我将数据从我的datagridview插入到我的数据库时,会在末尾插入一个不包含任何数据的额外行。我怎样才能克服这个问题。每次我在datagridview中单击一个新行并启动输入数据时,下面都会启用一个新行。有什么办法可以阻止这个空白行插入我的数据库吗?谢谢,这是我的代码......

                    Dim ds As New DataSet 
                    ds.Tables.Add("Main") 

                   Dim col As New DataColumn 

                    Dim dgvCol As New DataGridViewColumn 
                    For Each dgvCol In DataGridView1.Columns 
                        col = New DataColumn(dgvCol.Name) 
                        ds.Tables("Main").Columns.Add(col) 
                    Next 
                    Dim row As DataRow 
                    Dim colcount As Integer = DataGridView1.Columns.Count - 1 

                    For i As Integer = 0 To DataGridView1.Rows.Count - 1 
                        row = ds.Tables("Main").Rows.Add 

                        For Each column As DataGridViewColumn In DataGridView1.Columns 
                            row.Item(column.Index) = DataGridView1.Rows.Item(i).Cells(column.Index).Value 
                        Next 

                    Next 


                    Dim con As New SqlClient.SqlConnection 
                    Dim myCommand As New SqlClient.SqlCommand 

                    Dim dt As New DataTable() 
                    dt = ds.Tables("Main") 

                    For i As Integer = 0 To dt.Rows.Count - 1 

                        Dim myAdapter As New SqlClient.SqlDataAdapter 
                        Dim sql As String = "Insert into details (company, division, date, supplier, material_group, cost, dsc, email, userID, fullName, marketingCode, hccNumber, wbs, qty, currency, timestamp) VALUES ('" & DataGridView1.Rows(i).Cells(company.Name).Value & "', '" & DataGridView1.Rows(i).Cells(division.Name).Value & "','" & calendar & "', '" & DataGridView1.Rows(i).Cells(supplier.Name).Value & "', '" & DataGridView1.Rows(i).Cells(materialGroup.Name).Value & "', '" & DataGridView1.Rows(i).Cells(netprice.Name).Value & "', '" & DataGridView1.Rows(i).Cells(description.Name).Value & "', '" & cmbEmail.SelectedValue & "', '" & id & "', '" & newName & "', '" & DataGridView1.Rows(0).Cells(markCodes.Name).Value & "', '" & DataGridView1.Rows(0).Cells(hccNumber.Name).Value & "', '" & DataGridView1.Rows(i).Cells(wba.Name).Value & "', '" & DataGridView1.Rows(i).Cells(quantity.Name).Value & "', '" & DataGridView1.Rows(i).Cells(currency.Name).Value & "', '" & nowDate & "')" 
                        myCommand.Connection = con 
                        myCommand.CommandText = sql 
                        myAdapter.InsertCommand = myCommand 
                        myCommand.Connection.Open() 
                        myCommand.ExecuteNonQuery() 
                        myCommand.Connection.Close() 

                    Next.

2 个答案:

答案 0 :(得分:1)

您需要在循环中检查行中的值,在保存之前尝试检查DBNull.Value的每个单元格(或特定单元格)。或者,您可以将AllowUsersToAddRows设置为false以防止显示空白行。

答案 1 :(得分:0)

在循环中添加一个检查请参阅下面的示例代码

public static String encrypt(String plainString) throws Exception{
    byte[] encr  = AESBouncyCastle.encrypt(plainString.getBytes("UTF-8"), "secretKey");
    String encryptedString= Hex.encodeHexString(encr);
    return encryptedString;
}

public static String decrypt(String encryptedString) throws Exception{      
    byte[] retr = AESBouncyCastle.decrypt(Hex.decodeHex(encryptedString.toCharArray()), "secretKey");
    String decryptedString = new String(retr, "UTF-8");
    return decryptedString .trim();
}