Created
December 21, 2023 12:21
-
-
Save suniastar/f809f1eb64d098cd7c747842e991c63f to your computer and use it in GitHub Desktop.
.net 8.0 console application using System.Data.Odbc for converting absolute paths in pro.agzess.net's ARCHIV.MDB Bildatenbank to relative paths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Small program for converting old absolute paths in the Bilddatenbank of ARCHIV.MDB for pro.agzess.net to relative paths | |
// USE WITH CARE AND CREATE A BACKUP OF ARCHIV.MDB BEFORE RUNNING THIS PROGRAM | |
using System.Data.Odbc; | |
using (OdbcConnection conn = new OdbcConnection()) | |
{ | |
Console.WriteLine("Provide Path to ARCHIV.MDB."); | |
Console.Write("Name: "); | |
string input = Console.ReadLine(); | |
while (!File.Exists(input)) | |
{ | |
Console.WriteLine("ERR: File does not exist. Please try again."); | |
Console.Write("Name: "); | |
input = Console.ReadLine(); | |
} | |
conn.ConnectionString = | |
"Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=" + input + ";"; | |
conn.Open(); | |
Console.Write("Reading BildIds... "); | |
List<int> ids = new List<int>(); | |
using (OdbcCommand command = conn.CreateCommand()) | |
{ | |
command.CommandText = "SELECT BildId FROM Bilddatenbank"; | |
using (OdbcDataReader reader = command.ExecuteReader()) | |
{ | |
if (reader.FieldCount != 1 || !reader.GetName(0).Equals("BildId")) | |
{ | |
throw new Exception("Invalid Result Header"); | |
} | |
while (reader.Read()) | |
{ | |
ids.Add(reader.GetInt32(0)); | |
} | |
reader.Close(); | |
} | |
} | |
Console.WriteLine(ids.Count); | |
Console.WriteLine("Updating Paths..."); | |
foreach (int id in ids) | |
{ | |
string oldName, newName; | |
using (OdbcCommand command = conn.CreateCommand()) | |
{ | |
command.CommandText = "SELECT Dateiname FROM Bilddatenbank WHERE BildId = " + id; | |
using (OdbcDataReader reader = command.ExecuteReader()) | |
{ | |
if (reader.FieldCount != 1 || !reader.GetName(0).Equals("Dateiname")) | |
{ | |
throw new Exception("Invalid Result Header"); | |
} | |
if (!reader.Read()) | |
{ | |
throw new Exception("Command has no rows to read."); | |
} | |
oldName = reader.GetString(0).Trim(); | |
if (reader.Read()) | |
{ | |
throw new Exception("Command should only return one row."); | |
} | |
reader.Close(); | |
} | |
} | |
int index = oldName.IndexOf("\\AGZESS", StringComparison.OrdinalIgnoreCase); | |
if (index > 0) | |
{ | |
newName = oldName.Substring(index); | |
} | |
else | |
{ | |
newName = oldName; | |
} | |
Console.Write(id + ": Updating from " + oldName + " to " + newName + "..."); | |
using (OdbcCommand command = conn.CreateCommand()) | |
{ | |
command.CommandText = "UPDATE Bilddatenbank SET Dateiname = '" + newName + "' WHERE BildId = " + id; | |
using (OdbcDataReader reader = command.ExecuteReader()) | |
{ | |
if (reader.RecordsAffected != 1) | |
{ | |
throw new Exception("Updating " + id + " failed."); | |
} | |
reader.Close(); | |
} | |
} | |
Console.WriteLine("done"); | |
} | |
conn.Close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment