|Was up against the notorious, apostrophe issue, when sending a command line, which included an apostrophe, to a Database.
So after an enormous amount of Google'ing, I see now that it is recommended procedure to use parameterized commands, to do the trapping for you.
So I did just that.
Rewrote my code with the parameterized commands.
Simply put, my code is designed to read all file names off of a drives root, and enter them into a Database.
As you can see in my code, I store all of the file names into an Array, and then retrieve them one by one using a, FOREACH.
All well and good, and it does work, Line 36,
Console.WriteLine(fi.Name); proves it, but, Line 31,
cmd.Parameters.AddWithValue("@FileName", fi.Name); only grabs the very first filename, and then enters that first filename, as many times into the Database, as there are files on the root directory.
Why is Line 36, the Console Window line working perfectly, reflecting every single filename, but Line 31 only grabs the very first filename, and holds on to it?
static void Main(string args)
DriveInfo di = new DriveInfo(@"C:\");
string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=e:\\mydbtry.accdb";
OleDbConnection con = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
con.ConnectionString = conString;
DirectoryInfo dirInfo = di.RootDirectory;
FileInfo fileNames = dirInfo.GetFiles("*.*");
foreach (FileInfo fi in fileNames)
cmd.CommandText = "Insert into Table1 (FileName) Values (@FileName)";
cmd.Connection = con;