tags:

views:

135

answers:

2

I have a C# application that I am attempting to check my sqlite db if a filename already exists in the FileName column, if it DOESN'T exist, execute some code.. Here is what I am working with. To clarify - This code doesnt work.. it says I cannot convert insertCommand.ExecuteNonQuery to a string. I need to query the table and if the file name Does Not exist, then continue.

string[] files = Directory.GetFiles(@"C:\Documents and Settings\js91162\Desktop ",
        "R303717*.txt*", SearchOption.AllDirectories);
foreach (string file in files)
{
    string FileNameExt1 = Path.GetFileName(file);

    insertCommand.CommandText = @" 
            SELECT * FROM Import WHERE FileName == FileNameExt1;";
}
string contYes = insertCommand.ExecuteNonQuery();

if (string.IsNullOrEmpty(contYes))
{
    //more code
}

EDIT: added space in path so that slash doesn't eat quotation mark

A: 

ExecuteNonQuery is for non-queries. If you want to query data, use ExecuteReader

SqlDataReader myReader = insertCommand.ExecuteReader();
while(myReader.Read()) 
{
    Console.WriteLine(myReader.GetString(0)); //0 is the column-number
}
BlueRaja - Danny Pflughoeft
hmm. Ok, so this is returning a column number in the table? could I say, string myVar = myReader.GetString(0); and then use it for an if statement? - guess im confused on where the if comes in
jakesankey
@jake: I don't have any `if`'s, I don't know what you're talking about.
BlueRaja - Danny Pflughoeft
+1  A: 

If you want to check if there is a row with some file name then you could also use ExecuteScalar with

SELECT COUNT(*) FROM Import WHERE FileName = @FileName

Of course you must also set parameter for the command. Then you can do

int count = Convert.ToInt32(insertCommand.ExecuteScalar());
if (count == 0)
{
  // code...
}

EDIT: The whole thing with parameters would look something like this:

selectCommand.CommandText = "SELECT COUNT(*) FROM Import Where FileName = @FileName";
selectCommand.Parameters.Add("@FileName", SqlDbType.NVarChar); // Use appropriate db type here
insertCommand.CommandText = "INSERT INTO Import (FileName, ...) VALUES (@FileName, ...");
insertCommand.Parameters.Add("@FileName", SqlDbType.NVarChar);
// Add your other parameters here.
// ...
foreach (string file in files) 
{ 
  var fileName = Path.GetFileName(file);
  selectCommand.Parameters[0].Value = Path.GetFileName(fileName);
  int count = Convert.ToInt32(selectCommand.ExecuteScalar());
  if (count == 0)
  {
    // File does not exist in db, add it.
    insertCommand.Parameters[0].Value = fileName;
    // Init your other parameters here.
    // ...

    insertCommand.ExecuteNonQuery(); // This executes the insert statement.
  }
}
Patko
"Insufficient parameters supplied to the command" is the error i get for the int count = Convert.ToInt32 etc etc.. Missing something?
jakesankey
Yes, you are missing the parameter. "@FileName" is a parameter for the query. For that reason it is called a parameterized query. Look at http://www.aspnet101.com/2007/03/parameterized-queries-in-asp-net/ for an example.
Patko
Very good! It work, My only problem, and i have had this happen before, is that when I add that extra parameter, it throws off the columns in my db... All of the data is shifted over 1 column. Why is this?
jakesankey
What do you mean if throws off the columns? Select statement does not modify your db.
Patko
Ok, so now I am left with a working if statement, however, if the files are the same as those already in the db, it doesnt run. Which is what I want. BUT, if a new file is added, then it re-adds all of them, plus the new one.. How can i make it only add the new file??
jakesankey
Look at the updated answer with the insert statement. I hope it helps.Btw, it would really help if you would tell us in your question exactly what you are trying to do.
Patko
Thanks, sorry, I am new to this. I'm learning tho! "If i state everything in the question, then i wont need to have multiple threads".. go figure!~ thanks again
jakesankey