INSERT INTO SELECT执行查询,但未将数据插入数据库

时间:2018-10-28 10:03:50

标签: vb.net

嗨,我正在处理此查询,以将现有记录添加到同一数据库中,但指定了不同的数据。这是查询

Public Sub autoEnroll_by_Semester()
    'this is the connection string which is
    'in the enrollment_conn variable Data Source=192.168.254.108,1433;Network Library=DBMSSOCN;Initial Catalog = GEARS;User ID =user123;Password = user123"

    Try
        If MsgBox("Do you want to proceed with auto enrolling the records?" & Chr(13) & Chr(10) & "Please double check before proceeding!", vbInformation + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            enrollment_conn.Open()
            command = New SqlCommand("INSERT INTO dbo.Enrollees(RegistrationID,Track,Strand,Specialization_TVL,Grade,Section,Semester,School_Year,Transferee,Transferee_School,Transferee_Address,Transferred,Transfer_Type,Establishment_School,Date_Of_Transfer,Dropped,Date_Dropped,Regular,Gen_Average,Enrolled,Date_Enrolled) " & _
                                     "SELECT RegistrationID,Track,Strand,Specialization_TVL,Grade,Section,'" & Auto_Enroll_Semester.Specify_Cbx_semester.Text & "',School_Year,Transferee,Transferee_School,Transferee_Address,Transferred,Transfer_Type,Establishment_School,Date_Of_Transfer,Dropped,Date_Dropped,Regular,Gen_Average,Enrolled,'" & Auto_Enroll_Semester.Specify_Date_enrolled.Text & "'" & _
                                     " FROM dbo.Enrollees " & _
                                     "WHERE Enrolled='Yes' AND Semester='" & Auto_Enroll_Semester.Select_Cbx_semester.Text & "' AND School_Year='" & Auto_Enroll_Semester.Select_Cbx_schoolyear.Text & "' AND Section='Einstein'", enrollment_conn)
            command.ExecuteNonQuery()
            MsgBox("New records have been inserted.. Please check enrollees list", vbInformation)
        End If
    Catch ex As Exception
        MsgBox(ex.Message, vbExclamation)
    Finally
        command.Parameters.Clear()
        enrollment_conn.Close()
    End Try
End Sub

执行查询后,它执行但没有新记录。.我的问题是,当您使用控件作为值的基础时,我该如何执行该工作或不插入记录?还有其他可能的方法吗?谢谢

这是trace.writeline的结果: 插入dbo.Enrollees(RegistrationID,Track,Strand,Specialization_TVL,Grade,Section,Semester,School_Year,Transferee,Transferee_School,Transferee_Address,Transferred,Transfer_Type,Establishmentment_School,Date_Of_Transfer,Droped,Date_rolled,Date_rolled,Date_rolled,Date_rolled,Date_rolled,Date_roll ,跟踪,子线,专业化_TVL,成绩,部分,'12',学年,受让者,受让者学校,受让者地址,已转移,受让类型,建立学校,日期_转移,已删除,日期已删除,常规,平均_平均,已注册'10 / 30 .Enrollees WHERE Enrolled ='Yes'AND Semester = '11'AND School_Year ='2018-2019'AND Section ='Einstein';

2 个答案:

答案 0 :(得分:0)

它现在正在工作...问题是一个逻辑错误。.学期中where的值无效,并可能导致select语句无法选择记录。.谢谢大家的帮助。现在关闭了。

答案 1 :(得分:-1)

问题出在您的SQL语句上。第一个是有问题的-没什么意义。您说的是:“我希望您放入一个新记录,并为这些字段提供值”,但是实际上您并没有提供这些值。

INSERT INTO dbo.Enrollees(...)

应写为:

INSERT INTO dbo.Enrollees(...) VALUES (...);

换句话说,对于您的insert语句来说,它实际上意味着某些内容,您必须提供记录对所有这些字段的实际值。

对于您的查询,SQL的第二部分看起来不错。只需将值添加到上面的INSERT INTO语句中即可。记住也要添加分号。