I don't understand why everyone is trying to use ExecuteNonQuery or ExecuteScalar when the query in the question is a SELECT statement. If it was a stored procedure call that took care of the logic of INSERT versus UPDATE based on the existence of a value, the ExecuteScalar would make sense because you can return whatever single value you want from a stored procedure.
However, given the structure of the question, I'm leaning towards this as the answer.
// Automatically dispose the connection when done
using(SqlConnection connection = new SqlConnection(sqlConnection.ConnectionString)) {
try {
connection.Open();
// query to check whether value exists
string sql = @"SELECT dataset1
FROM dbo.ste
WHERE project = 'whatever'
AND date = '2010-11-30'";
// create the command object
using(SqlCommand command = new SqlCommand(sql, connection)) {
using(SqlDataReader reader = command.ExecuteReader()) {
// if the result set is not NULL
if(reader.HasRows) {
// update the existing value + the value from the text file
}
else {
// insert a value from a text file
}
}
}
}
finally {
// always close connection when done
if(connection.State != ConnectionState.Closed) {
connection.Close();
}
}
}
You can change the query to use WHERE EXISTS if you don't want to stream back full matches, but from the sounds of it, you would only have at most 1 match anyways.