我有一个C#windows应用程序,有时应用程序将用于从excel文件导入记录,我可以在我的开发机器上使用批量插入在一分钟内导入2500条记录,当我尝试正常的常规插入方法时,它也花了插入2500条记录的同一分钟,在我部署应用程序并安装在客户端的机器上后,这变得更糟,导入相同数量的记录需要两个多小时,我感觉因为我正在使用批量插入,速度应该大大优化,下面是我的代码:
public int InsertTaxRecordBatchTransaction(IEnumerable<EmployeeDetails> employeeDetails)
{
SQLiteConnection conn = null;
SQLiteCommand cmd = null;
var results = new List<int>();
try
{
string insert = "INSERT INTO IncomeTax (OrganisationId,OrganisationName,StaffId,StaffName,Department,Position,NoOfMonthsWorked,TaxIdNumber,SessionId,Month,Year,BasicSalaryVALUES (@OrganisationId,@OrganisationName,@StaffId,@StaffName,@Department,@Position,@NoOfMonthsWorked,@TaxIdNumber,@SessionId,@Month,@Year,@BasicSalary);";
using (conn = new SQLiteConnection(connstring))
{
conn.Open();
using (var transaction = conn.BeginTransaction())
{
try
{
using (cmd = conn.CreateCommand())
{
//cmd = new SQLiteCommand(insert, conn);
cmd.CommandType = CommandType.Text;
cmd.CommandText = insert;
cmd.CommandTimeout = 900000000;
cmd.Parameters.Add("@OrganisationId", DbType.String, 50).Value = OrganisationId;
cmd.Parameters.Add("@OrganisationName", DbType.String, 50).Value = OrganisationName;
cmd.Parameters.Add("@StaffID", DbType.String, 50).Value = StaffId;
cmd.Parameters.Add("@StaffName", DbType.String, 50).Value = StaffName;
cmd.Parameters.Add("@Department", DbType.String, 50).Value = Department;
cmd.Parameters.Add("@Position", DbType.String, 50).Value = Position;
cmd.Parameters.Add("@NoOfMonthsWorked", DbType.Int16, 5).Value = NoOfMonthsWorked;
cmd.Parameters.Add("@TaxIdNumber", DbType.String, 5).Value = TaxIdNumber;
cmd.Parameters.Add("@SessionId", DbType.String, 20).Value = SessionId;
cmd.Parameters.Add("@Month", DbType.String, 20).Value = Month;
cmd.Parameters.Add("@Year", DbType.String, 20).Value = Year;
cmd.Parameters.Add("@BasicSalary", DbType.Decimal, 2).Value = BasicSalary;
foreach (var employee in employeeDetails)
{
cmd.Parameters.Add("@OrganisationId", DbType.String, 50).Value = employee.OrganisationId;
cmd.Parameters.Add("@OrganisationName", DbType.String, 50).Value = employee.OrganisationName;
cmd.Parameters.Add("@StaffID", DbType.String, 50).Value = employee.StaffId;
cmd.Parameters.Add("@StaffName", DbType.String, 50).Value = employee.StaffName;
cmd.Parameters.Add("@Department", DbType.String, 50).Value = employee.Department;
cmd.Parameters.Add("@Position", DbType.String, 50).Value = employee.Position;
cmd.Parameters.Add("@NoOfMonthsWorked", DbType.Int16, 5).Value = employee.NoOfMonthsWorked;
cmd.Parameters.Add("@TaxIdNumber", DbType.String, 5).Value = employee.TaxIdNumber;
cmd.Parameters.Add("@SessionId", DbType.String, 20).Value = employee.SessionId;
cmd.Parameters.Add("@Month", DbType.String, 20).Value = employee.Month;
cmd.Parameters.Add("@Year", DbType.String, 20).Value = employee.Year;
cmd.Parameters.Add("@BasicSalary", DbType.Decimal, 2).Value = employee.BasicSalary;
results.Add(cmd.ExecuteNonQuery());
}
}
transaction.Commit();
DspMsg = "Tax computed successfully!";
}
catch(Exception ex)
{
transaction.Rollback();
DspMsg = ex.Message+ " some errors have occured, no record was saved! ";
}
}
}
// conn.Close();
}
catch (SQLiteException ex)
{
DspMsg = ex.Message;
}
return results.Sum();
}
我不知道我哪里弄错了,我将不胜感激。