Click here to Skip to main content
15,878,809 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: WPF .Net Core Dependency Injection & View Models Pin
Graeme_Grant31-Dec-22 3:15
mvaGraeme_Grant31-Dec-22 3:15 
Question(ANSWERED) (newbie) I'm confused about ListView ItemSource bindings. Pin
Maximilien8-Dec-22 2:42
Maximilien8-Dec-22 2:42 
AnswerRe: (newbie) I'm confused about ListView ItemSource bindings. Pin
Richard Deeming8-Dec-22 3:58
mveRichard Deeming8-Dec-22 3:58 
GeneralRe: (newbie) I'm confused about ListView ItemSource bindings. Pin
Maximilien8-Dec-22 4:22
Maximilien8-Dec-22 4:22 
Questionfast directory infos for a lot of files Pin
pitwi6-Dec-22 2:14
pitwi6-Dec-22 2:14 
AnswerRe: fast directory infos for a lot of files Pin
pitwi6-Dec-22 2:29
pitwi6-Dec-22 2:29 
GeneralRe: fast directory infos for a lot of files Pin
Dave Kreskowiak6-Dec-22 5:40
mveDave Kreskowiak6-Dec-22 5:40 
AnswerRe: fast directory infos for a lot of files Pin
Richard Deeming6-Dec-22 2:51
mveRichard Deeming6-Dec-22 2:51 
Well, that code is ... interesting. There's a lot of needless work going on there.

Try something like this instead:
C#
private void loopDir(DirectoryInfo folder, DataTable table, List<string> errors)
{
    try
    {
        foreach (DirectoryInfo subFolder in folder.EnumerateDirectories())
        {
            loopDir(subFolder, table, errors);
        }
        
        string pfad = folder.FullName.Substring(2);
        foreach (FileInfo file in folder.EnumerateFiles())
        {
            string extension = Path.GetExtension(file.Name);
            if (!fext.Contains(extension)) continue;
            
            table.Rows.Add(pfad, file.Name);
        }
    }
    catch (Exception ex)
    {
        errors.Add(ex.Message);
    }
}

private int ImportFiles(string initialFolder, List<string> errors)
{
    DirectoryInfo folder = new DirectoryInfo(initialFolder);
    DataTable table = new DataTable();
    table.Columns.Add("Pfad", typeof(string));
    table.Columns.Add("Datei", typeof(string));
    loopDir(folder, table, errors);
    
    if (table.Rows.Count == 0)
    {
        MessageBox.Show("No files found to import.", "Import", MessageBoxButton.OK, MessageBoxImage.Error);
        return 0;
    }
    
    using (SQLiteTransaction dbTrans = dbConn.BeginTransaction())
    {
        using (SQLiteCommand dbCom = new SQLiteCommand("DELETE FROM Dateien", dbConn, dbTrans))
        {
            dbCom.ExecuteNonQuery();
        }
        
        using (SQLiteCommand dbCom = new SQLiteCommand("INSERT INTO Dateien (Pfad, Datei) VALUES ($Pfad, $Datei)", dbConn, dbTrans))
        {
            SQLiteParameter pPfad = dbCom.CreateParameter();
            pPfad.ParameterName = "$Pfad";
            dbCom.Parameters.Add(pPfad);
            
            SQLiteParameter pDatei = dbCom.CreateParameter();
            pDatei.ParameterName = "$Datei";
            dbCom.Parameters.Add(pDatei);
            
            foreach (DataRow row in table.Rows)
            {
                pPfad.Value = row["Pfad"];
                pDatei.Value = row["Datei"];
                dbCom.ExecuteNonQuery();
            }
        }
        
        transaction.Commit();
    }
    
    return table.Rows.Count;
}

private void ButtonImport_Click(object sender, RoutedEventArgs e)
{
    Cursor cu = this.Cursor;
    this.Cursor = Cursors.Wait;
    try
    {
        const string initdir = @"H:\mp3\";
        List<string> errors = new List<string>();
        
        Stopwatch sw = Stopwatch.StartNew();
        int recordCount = ImportFiles(initdir);
        sw.Stop();
        
        string message = $"{recordCount} files saved in {sw.Elapsed.TotalMinutes:0.0} minutes.";
        if (errors.Count != 0) message += Environment.NewLine + Environment.NewLine + string.Join(Environment.NewLine, errors);

        MessageBox.Show(message, "Import", MessageBoxButton.OK, MessageBoxImage.Information);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Import Error", MessageBoxButton.OK, MessageBoxImage.Error);
    }
    finally
    {
        this.Cursor = cu;
    }
}

NB: SQLite doesn't have a "bulk insert" option, so you need to use a transaction and a single SQLiteCommand to perform the insert:
Bulk insert - Microsoft.Data.Sqlite | Microsoft Learn[^]



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


modified 7-Dec-22 4:38am.

GeneralRe: fast directory infos for a lot of files Pin
pitwi6-Dec-22 6:56
pitwi6-Dec-22 6:56 
QuestionCustom Control Styling Pin
Kevin Marois2-Dec-22 8:58
professionalKevin Marois2-Dec-22 8:58 
AnswerRe: Custom Control Styling Pin
Richard Deeming5-Dec-22 0:27
mveRichard Deeming5-Dec-22 0:27 
GeneralRe: Custom Control Styling Pin
Kevin Marois5-Dec-22 12:15
professionalKevin Marois5-Dec-22 12:15 
GeneralRe: Custom Control Styling Pin
Richard Deeming5-Dec-22 21:57
mveRichard Deeming5-Dec-22 21:57 
GeneralRe: Custom Control Styling Pin
Kevin Marois5-Dec-22 13:57
professionalKevin Marois5-Dec-22 13:57 
GeneralRe: Custom Control Styling Pin
Kevin Marois5-Dec-22 14:10
professionalKevin Marois5-Dec-22 14:10 
QuestionWPF .Net Core Relay Command with Parameters Pin
Kevin Marois1-Dec-22 13:50
professionalKevin Marois1-Dec-22 13:50 
QuestionForgot Password Pin
Kevin Marois1-Dec-22 13:06
professionalKevin Marois1-Dec-22 13:06 
AnswerRe: Forgot Password Pin
Richard Deeming1-Dec-22 22:10
mveRichard Deeming1-Dec-22 22:10 
QuestionWPF Core Hyperlkink Custom Control Pin
Kevin Marois29-Nov-22 16:29
professionalKevin Marois29-Nov-22 16:29 
AnswerRe: WPF Core Hyperlkink Custom Control Pin
Richard Deeming29-Nov-22 21:59
mveRichard Deeming29-Nov-22 21:59 
GeneralRe: WPF Core Hyperlkink Custom Control Pin
Kevin Marois30-Nov-22 5:46
professionalKevin Marois30-Nov-22 5:46 
GeneralRe: WPF Core Hyperlkink Custom Control Pin
Richard Deeming30-Nov-22 21:25
mveRichard Deeming30-Nov-22 21:25 
QuestionPath Images Pin
Kevin Marois29-Nov-22 14:57
professionalKevin Marois29-Nov-22 14:57 
AnswerRe: Path Images Pin
Richard Deeming29-Nov-22 23:46
mveRichard Deeming29-Nov-22 23:46 
QuestionDropShadowEffect Above & Below Pin
Kevin Marois29-Nov-22 14:30
professionalKevin Marois29-Nov-22 14:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.