I have the below code, and I am wondering if there is a more efficient and faster way of doing something like this for scaling purposes. This is a monitoring program, that checks for files and if it doesn't find the record already in the database, then it inserts it. But as we add on more and more directories to monitor I'm wondering about performance issues. Is there a faster way to check then using the an if not exists followed by an insert statement all in one sql command? Thank you for any suggestions.
SqlCommand cmd = new SqlCommand("if not exists (select File_Name from [table].[dbo].[tablename] " +
"where Workflow_Step_Name=@iWorkflow_Step_Name and File_Name=@latestfile) INSERT INTO " +
"[table].[dbo].[tablename] (Workflow_Name,Workflow_Step_Name,Creation_Date,File_Name,File_Path,File_Size,Insert_TimeStamp,Workflow_Id,Batch_Id) " +
"Values(@iWorkflow_Name,@iWorkflow_Step_Name,@lastUpdated,@latestfile,@iFolder_Path,@fileSize,@timestamp,@iworkflow_Id,@batch_id)");
cmd.Connection = myConnection;
cmd.Parameters.AddWithValue("@iWorkflow_Name", i.Workflow_Name);
cmd.Parameters.AddWithValue("@iWorkflow_Step_Name", i.Workflow_Step_Name);
cmd.Parameters.AddWithValue("@lastUpdated", lastupdated);
cmd.Parameters.AddWithValue("@latestfile", latestfile);
cmd.Parameters.AddWithValue("@iFolder_Path", i.Folder_Path);
cmd.Parameters.AddWithValue("@fileSize", fileSize);
cmd.Parameters.AddWithValue("@timestamp", DateTime.UtcNow);
cmd.Parameters.AddWithValue("@iworkflow_Id", i.Workflow_Id);
cmd.Parameters.AddWithValue("@batch_id", batchId);
myConnection.Open();
cmd.ExecuteNonQuery();
myConnection.Close();