INSERT语句中的列数少于值条款中的列数

时间:2015-05-26 06:23:53

标签: sql-server insert transactions identity

我在SQL Server中有一个包含34列的表。标识列是自动递增的主键,因此不包含在INSERT语句中。因此,我的INSERT语句中有33个参数。检查,仔细检查并交叉检查我的INSERT语句和表后,我不断收到上述错误。

这是我的INSERT声明:

db.Execute("InsertFile1Transaction", "INSERT INTO File1 (candidate_id, "
            + "first_name, last_name, nick_name, middle_name, date_of_birth, "
            + "place_of_birth, nationality_code, gender_code, civil_status_code, "
            + "affix_code, religion_code, blood_type, sss_number, tin_number, "
            + "present_address, present_city, present_district, present_country_code, "
            + "present_telephone_home, present_telephone_additional, "
            + "present_telephone_mobile, present_fax, permanent_address, "
            + "permanent_city, permanent_district, permanent_country_code, "
            + "provincial_address, provincial_city, provincial_district, "
            + "provincial_country_code, provincial_telephone, is_complete) "
            + "VALUES ('" + candidateId + "','" + firstName + "','" + lastName + "','"
            + nickName + "','" + middleName + "','" + dateOfBirth + "','"
            + placeOfBirth + "','" + nationality + "','" + gender + "','"
            + civilStatus + "','" + affix + "','" + religion + "','"
            + bloodType + "','" + sss + "','" + tin + "','" + "','"
            + presentAddress + "','" + presentCity + "','" + presentDistrict + "','"
            + presentCountry + "','" + presentTelephoneHome + "','"
            + presentTelephoneAdditional + "','" + presentTelephoneMobile + "','"
            + presentFax + "','" + permanentAddress + "','" + permanentCity + "','"
            + permanentDistrict + "','" + permanentCountry + "','"
            + provincialAddress + "','" + provincialCity + "','"
            + provincialDistrict + "','" + provincialCountry + "','"
            + provincialTelephone + "','" + isComplete + "')");

DB class

public void Execute(string transactionName, string sqlStatement)
    {
        SqlConnection sqlConnection = new SqlConnection(GetConnectionString());
        sqlConnection.Open();
        SqlTransaction sqlTransaction = sqlConnection.BeginTransaction(transactionName);
        try
        {
            SqlCommand com = new SqlCommand(sqlStatement, sqlConnection, sqlTransaction);
            com.ExecuteNonQuery();
            sqlTransaction.Commit();
            Debug.WriteLine(transactionName + " transaction commit success.");
        }
        catch (Exception e1)
        {
            Debug.WriteLine(transactionName + " Commit Exception Type: " + e1.GetType() +
                "\nMessage: " + e1.Message +
                "\nStack Trace: " + e1.StackTrace);
            try
            {
                sqlTransaction.Rollback();
                Debug.WriteLine(transactionName + " transaction rollback success.");
            }
            catch (Exception e2)
            {
                Debug.WriteLine(transactionName + " Rollback Exception Type: " + e2.GetType() +
                    "\nMessage: " + e2.Message +
                    "\nStack Trace: " + e2.StackTrace);
            }
            finally
            {
                sqlTransaction.Dispose();
            }
        }
        finally
        {
            sqlTransaction.Dispose();
            sqlConnection.Close();
        }
    }

此外,当我删除I​​NSERT语句中的列列表时,我得到一个不同的错误:"只有在使用列列表且IDENTITY_INSERT为ON时才能插入标识列的显式值。

谢谢!

1 个答案:

答案 0 :(得分:3)

tinpresentAddress之间有双逗号:

 sss + "','" + tin + "','" + "','" + presentAddress