SQL Query

  • Thread starter Thread starter Aaron
  • Start date Start date
A

Aaron

My db table looks like this
FILENAME FILE_CREATED FILE_MODIFIED
test 9/12
test2 9/13 10/13
..
..
all FILE_CREATED field have value, only some FILE_MODIFIED have value.

I need to get the value of FILENAME column and this is what i have

SELECT FILENAME FROM REW_FILES WHERE FILE_CREATED =@cDate AND
FILE_MODIFIED = @mDate

This problem with this query is that it would return null if the
FILE_MODIFIED field is empty.

How can I write a query that ignores " AND FILE_MODIFIED = @mDate" if
FILE_MODIFIED is null?

Thanks
 
Try placing your query results in a variable. Then test your variable if it
contains records or not.


public void CreateMySqlDataReader(string mySelectQuery,string
myConnectionString)
{
//Place your query in a string
string mySelectQuery = "SELECT FILENAME FROM REW_FILES WHERE
FILE_CREATED =@cDate OR FILE_MODIFIED = @mDate";

//Create a connection
SqlConnection myConnection = new SqlConnection(myConnectionString);

//Create a Sql Command and pass your string query.
SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);

// Open your connection
myConnection.Open();

// Place all results in a SqlDataReader (This acts as your variable
container)
SqlDataReader myReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection);

//Test if it has rows
if (myReader.HasRows)
{
while(myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
}
}

//
// Do your other coding here....
//

//Implicitly closes the connection because
CommandBehavior.CloseConnection was specified.
myReader.Close();
}


Hope this helps! :D

Ann :D
 
Back
Top