Sqlite批量保存数据,做Excel数导入Sqlite用

/// <summary>
/// 保存DataTable
/// </summary>
/// <param name="dt">datatable</param>
/// <returns>影响的行数</returns>
public static int SaveDataTable(DataTable dt,string dbName)
{
    SQLiteTransaction _tran = null;
    try
    {  
    using (SQLiteConnection connection = GetSQLiteConnection(dbName))
    {
        DbProviderFactory dataFactory = SQLiteFactory.Instance;

        DbDataAdapter adapter = dataFactory.CreateDataAdapter();

        
        string sql = string.Format(@"select * from {0} where 1=2", dt.TableName);

        DbCommand cmd = connection.CreateCommand();

        DbCommandBuilder comBuilder = dataFactory.CreateCommandBuilder(); 

        comBuilder.DataAdapter = adapter;

        cmd.CommandText = sql;

        cmd.Connection = connection;

        adapter.SelectCommand = cmd;

        adapter.InsertCommand = comBuilder.GetInsertCommand();

        adapter.UpdateCommand = comBuilder.GetUpdateCommand();

        adapter.DeleteCommand = comBuilder.GetDeleteCommand();

        connection.Open();

        _tran = connection.BeginTransaction();//transaction begin 传说中sqlite每执行一条insert语句都开启一个事务,死慢

        cmd.Transaction = _tran;

        int result = adapter.Update(dt);

        _tran.Commit();//transaction end

        return result;
        }
    }

    catch (Exception ex)
    {
        _tran.Rollback();
        throw new Exception("保存DataTable出错:" + ex.Message);
    }
    finally
    {
               
    }
}