列数与第1行的值数不匹配

时间:2019-06-14 07:14:39

标签: mysql vb.net mariadb

我的代码有问题,我已经尝试为每一列放入指定的表,我的代码的哪一部分有问题?

DAOFactory dao = org.mockito.Mockito.mock(DAOFactory.class);
SomeClass factoryMock = mock(SomeClass.class); 
when(dao.getDAOFactory()).thenReturn(factoryMock);
when(factoryMock.getCountryDAO()).thenReturn((CountryDAO)iMockCountryDAO);

1 个答案:

答案 0 :(得分:0)

正如@jmcilhinney所评论的那样,将您的插入查询更改为使用参数,因为它将更易于阅读,并确保值的数量与列的数量匹配。

以下是一些“模板”代码供您扩展:

Dim InsertCmdText As String =
    "Insert into tbl_clients(clients_record_num,clients_client_id,clients_name,clients_contact_number,clients_address,clients_industry,clients_status,clients_delegate,clients_notes) " &
    "values (@clients_record_num,@clients_client_id,@clients_name,@clients_contact_number,@clients_address,@clients_industry,@clients_status,@clients_delegate,@clients_notes)"
Dim SqlConnectionString As String = "Your Connection Info"
Using cmd As New SqlCommand(InsertCmdText, New SqlConnection(SqlConnectionString))
    Try
        cmd.Parameters.AddWithValue("@clients_record_num", "Value Needed")
        cmd.Parameters.AddWithValue("@clients_client_id", "Value Needed")
        cmd.Parameters.AddWithValue("@clients_name", "Value Needed")
        'repeat above for all params

        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
    Catch ex As Exception
        'Handle your exception
    Finally
        cmd.Connection.Close()
    End Try
End Using

或者使用Parameters.Add代替AddWithValue。有关每种优点的讨论,请参见SqlCommand Parameters Add vs. AddWithValue