Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to use RegEx for AlphaNumeric expressions Pin
Dave Kreskowiak28-Nov-17 10:52
mveDave Kreskowiak28-Nov-17 10:52 
AnswerRe: How to use RegEx for AlphaNumeric expressions Pin
Gerry Schmitz29-Nov-17 4:52
mveGerry Schmitz29-Nov-17 4:52 
QuestionEntity FrameWork Question Pin
Kevin Marois28-Nov-17 6:08
professionalKevin Marois28-Nov-17 6:08 
AnswerRe: Entity FrameWork Question Pin
Richard MacCutchan28-Nov-17 6:11
mveRichard MacCutchan28-Nov-17 6:11 
GeneralRe: Entity FrameWork Question Pin
Kevin Marois28-Nov-17 6:15
professionalKevin Marois28-Nov-17 6:15 
GeneralRe: Entity FrameWork Question Pin
Richard MacCutchan28-Nov-17 6:18
mveRichard MacCutchan28-Nov-17 6:18 
GeneralRe: Entity FrameWork Question Pin
Kevin Marois28-Nov-17 6:19
professionalKevin Marois28-Nov-17 6:19 
QuestionParameterized Command Not Adding To DB Pin
MsJane27-Nov-17 8:21
MsJane27-Nov-17 8:21 
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;
            con.Open();
                        
            DirectoryInfo dirInfo = di.RootDirectory;         
            FileInfo[] fileNames = dirInfo.GetFiles("*.*");  // Store all "root" file names in Array

            foreach (FileInfo fi in fileNames)              
            {

                cmd.CommandText = "Insert into Table1 (FileName) Values (@FileName)"; 

                cmd.Parameters.AddWithValue("@FileName", fi.Name);

                cmd.Connection = con;
                cmd.ExecuteNonQuery();

                Console.WriteLine(fi.Name);  // Verify via Console window that the "fi" variable really is changing
                
            }

            con.Close();

            Console.WriteLine("Finished.");
            Console.ReadKey();
        }

AnswerRe: Parameterized Command Not Adding To DB Pin
Sascha Lefèvre27-Nov-17 8:54
professionalSascha Lefèvre27-Nov-17 8:54 
AnswerRe: Parameterized Command Not Adding To DB Pin
Richard MacCutchan27-Nov-17 22:06
mveRichard MacCutchan27-Nov-17 22:06 
AnswerRe: Parameterized Command Not Adding To DB Pin
MsJane30-Nov-17 5:13
MsJane30-Nov-17 5:13 
Questionhow to get programmatically the selected item of a treeview dynamically built Pin
Hervend27-Nov-17 2:02
Hervend27-Nov-17 2:02 
AnswerRe: how to get programmatically the selected item of a treeview dynamically built Pin
Eddy Vluggen27-Nov-17 2:21
professionalEddy Vluggen27-Nov-17 2:21 
GeneralRe: how to get programmatically the selected item of a treeview dynamically built Pin
Hervend27-Nov-17 2:56
Hervend27-Nov-17 2:56 
GeneralRe: how to get programmatically the selected item of a treeview dynamically built Pin
Eddy Vluggen27-Nov-17 3:26
professionalEddy Vluggen27-Nov-17 3:26 
GeneralRe: how to get programmatically the selected item of a treeview dynamically built Pin
Hervend27-Nov-17 19:19
Hervend27-Nov-17 19:19 
GeneralRe: how to get programmatically the selected item of a treeview dynamically built Pin
Gerry Schmitz27-Nov-17 8:12
mveGerry Schmitz27-Nov-17 8:12 
QuestionWorkaround for a virtual method call in the constructor Pin
CodeWraith25-Nov-17 9:40
CodeWraith25-Nov-17 9:40 
Questionissues concerning c# object reference not set to an instance of an object error Pin
Hervend24-Nov-17 2:27
Hervend24-Nov-17 2:27 
AnswerRe: issues concerning c# object reference not set to an instance of an object error Pin
Eddy Vluggen24-Nov-17 2:49
professionalEddy Vluggen24-Nov-17 2:49 
GeneralRe: issues concerning c# object reference not set to an instance of an object error Pin
Hervend24-Nov-17 3:25
Hervend24-Nov-17 3:25 
GeneralRe: issues concerning c# object reference not set to an instance of an object error Pin
Eddy Vluggen24-Nov-17 3:33
professionalEddy Vluggen24-Nov-17 3:33 
GeneralRe: issues concerning c# object reference not set to an instance of an object error Pin
Hervend24-Nov-17 4:24
Hervend24-Nov-17 4:24 
GeneralRe: issues concerning c# object reference not set to an instance of an object error Pin
Hervend24-Nov-17 4:35
Hervend24-Nov-17 4:35 
GeneralRe: issues concerning c# object reference not set to an instance of an object error Pin
Eddy Vluggen24-Nov-17 5:15
professionalEddy Vluggen24-Nov-17 5:15 

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.