插入语句以将数据添加到数据库

时间:2014-11-18 06:36:17

标签: mysql vb.net ms-access access-vba

我似乎没有发现我的查询有什么问题。末尾的值是变量的名称

Command.CommandText = (" INSERT INTO [TransfersAndAdditionalCourses]
               (TransferCourse1,TransferCourse2, TransferCourse3, 
               TransferCourse4, TransferCourse5, AdditionalCourse1,   
          AdditionalCourse2, AdditionalCourse3, AdditionalCourse4, AdditionalCourse5) 
          VALUES ('" & firstTransferCourse & ",' " & secondTransferCourse 
          & "," & thirdTransferCourse & " ', '" & fourthTransferCourse & " ','" 
          & fifthTransferCourse & " ', '" & firstAdditionalCourse & ", " 
          & secondAdditionalCourse & "',' " & thirdAdditionalCourse & "',' " 
          & fourthAdditionalCourse & "','" & fifthAdditionalCourse & "' ")

由于

2 个答案:

答案 0 :(得分:0)

我似乎错过了一些单引号。

请试试这个:

Command.CommandText = ("INSERT INTO [TransfersAndAdditionalCourses]
           (TransferCourse1,TransferCourse2, TransferCourse3, TransferCourse4, 
            TransferCourse5, AdditionalCourse1, AdditionalCourse2, AdditionalCourse3,
            AdditionalCourse4, AdditionalCourse5)
    VALUES ('" & firstTransferCourse & "',' " & secondTransferCourse & "', '" 
               & thirdTransferCourse & " ', '" & fourthTransferCourse & " ','" 
               & fifthTransferCourse & " ', '" & firstAdditionalCourse & "', '" 
               & secondAdditionalCourse & "',' " & thirdAdditionalCourse & "',' " 
               & fourthAdditionalCourse & "','" & fifthAdditionalCourse & "' ")

答案 1 :(得分:0)

设置插入查询的格式,您将看到错误,在许多地方缺少引号,也不需要前导(和拖尾)。它应该看起来像

Command.CommandText = 
"
INSERT INTO [TransfersAndAdditionalCourses] 
(
    TransferCourse1,
    TransferCourse2, 
    TransferCourse3, 
    TransferCourse4, 
    TransferCourse5, 
    AdditionalCourse1,  
    AdditionalCourse2, 
    AdditionalCourse3, 
    AdditionalCourse4, 
    AdditionalCourse5
) 
VALUES 
(
    '" & firstTransferCourse & "', 
    '" & secondTransferCourse & "', 
    '" & thirdTransferCourse & "', 
    '" & fourthTransferCourse & "',
    '" & fifthTransferCourse & "', 
    '" & firstAdditionalCourse & "', 
    '" & secondAdditionalCourse & "',
    '" & thirdAdditionalCourse & "',
    '" & fourthAdditionalCourse & "',
    '" & fifthAdditionalCourse & "'
)"