我有一个班级 People_val。我在另一个班级列出了这个班级。现在我在这个列表上做一个 foreach 并且对于每个项目我调用它的 ToSQL() 函数,它给了我一个插入查询。列表计数超过 50,000 条记录。第 9774 个查询未完全形成。 9771 INSERT INTO CONVERSION_PEOPLE_VALIDATION VALUES(16413,'Jnto','Johnson',因此命令未执行并且事务回滚。我应该怎么做才能在 foreach 中获取完整的字符串。
public class People_Val
{
public int MEMBERS_ID;
public string PFirstName;
public string PLastName;
public DateTime? BIRTH_DATE;
public string RELATIONSHP;
public string ToSQL()
{
return string.Format("INSERT INTO CONVERSION_PEOPLE_VALIDATION VALUES ({0},'{1}','{2}','{3}', TO_CHAR('{4}', 'DD-MON-YYYY'))",
MEMBERS_ID, PFirstName.Replace("'","\""), PLastName.Replace("'","\""), RELATIONSHP, BIRTH_DATE);
}
}
public void insertPeopleValTrans(List<People_Val> _lstPeoplevals)
{
int count=0;
TextWriter tw = new StreamWriter(@"C:\PeopleVal.txt");
using (OracleConnection conn = new OracleConnection(connectionStr)) {
conn.Open();
OracleTransaction trans;
trans = conn.BeginTransaction();
try {
foreach (People_Val peopleVal in _lstPeoplevals) {
OracleCommand cmd = new OracleCommand(peopleVal.ToSQL(), conn, trans);
cmd.ExecuteNonQuery();
count++;
if (count % 500 == 0) {
Console.WriteLine("saving " + count.ToString() + " Records");
}
tw.WriteLine(count.ToString()+" "+ peopleVal.ToSQL());
}
trans.Commit();
tw.Close();
} catch (Exception ex) {
Console.WriteLine(ex.Message);
trans.Rollback();
}
conn.Close();
}
}