INSERT 语句中的列数少于值子句中的列数
Fewer Columns In INSERT Statement Than In Values Clause
我在 SQL 服务器中有一个 table,有 34 列。标识列是一个自动递增的主键,因此不包含在 INSERT 语句中。因此,我的 INSERT 语句中有 33 个参数。在检查、仔细检查和交叉检查我的 INSERT 语句和 table 之后,我不断收到上述错误。
这是我的 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();
}
}
此外,当我删除 INSERT 语句中的列列表时,我收到一个不同的错误消息:“只有在使用列列表且 IDENTITY_INSERT 为 ON 时,才能插入标识列的显式值.
谢谢!
tin
和 presentAddress
之间有双逗号:
sss + "','" + tin + "','" + "','" + presentAddress
我在 SQL 服务器中有一个 table,有 34 列。标识列是一个自动递增的主键,因此不包含在 INSERT 语句中。因此,我的 INSERT 语句中有 33 个参数。在检查、仔细检查和交叉检查我的 INSERT 语句和 table 之后,我不断收到上述错误。
这是我的 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();
}
}
此外,当我删除 INSERT 语句中的列列表时,我收到一个不同的错误消息:“只有在使用列列表且 IDENTITY_INSERT 为 ON 时,才能插入标识列的显式值.
谢谢!
tin
和 presentAddress
之间有双逗号:
sss + "','" + tin + "','" + "','" + presentAddress