0

really a simple question , I have two Variables "one_hour_ago" and "current_time", I need to pass these two variables in my sql command like :

string commandString = "SELECT * from myTable where time between one_hour_ago and current_time";

here is what I have but I get syntax error

string commandString = "SELECT * from myTable where TS between ' and /" + one_hour_ago + "'" + current_time + "/"; 

Thanks

2
  • 4
    Look at the SqlParameter class on MSDN and at their use via SqlCommand class Commented Jun 4, 2012 at 22:16
  • don't forget to accept an answer if it helped you, as a basic courtesy towards others... Commented Jun 7, 2012 at 1:01

3 Answers 3

7
string sqlString = "SELECT * FROM myTable WHERE time BETWEEN  @before AND @current_time"; 
SqlCommand oCmd = new SqlCommand(sqlString , connString);
oCmd.Parameters.AddWithValue("@before", date_before);
oCmd.Parameters.AddWithValue("@current_time", currentTime);

where date_before and currentTime are the parameters you pass to the method.

this should take care of the sql injection stuff

Sign up to request clarification or add additional context in comments.

2 Comments

@user1429595: I too suggest you go with this answer, mine is just a quick and dirty fix... +1
using Parameters should also help prevent SQL injection (although isn't the be all and end all)
0
create procedure Proc_name (@param1 varchar(100), 
@param2 varchar(100), 
@param3 varchar(100), 
@param4 varchar(100)) 
as 
insert into table1 values(@param1, @param2, @param3, @param4)

Then from your code (giving a c# example using ADO.NET)

using (SqlConnection connection = new SqlConnection(connectionString))
{
// Create the command and set its properties.
SqlCommand command = new SqlCommand();
SqlCommand command = new SqlCommand 
   ("Proc_name", connection); 

command.CommandType = CommandType.StoredProcedure;

// Add the input parameters and set the properties.
SqlParameter parameter1 = new SqlParameter();
parameter.ParameterName = "@Param1";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = param1;

SqlParameter parameter2 = new SqlParameter();
parameter.ParameterName = "@Param2";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = param2;




// Add the parameter to the Parameters collection. 
command.Parameters.Add(parameter1);
command.Parameters.Add(parameter2);


// Open the connection and execute the reader.
connection.Open();
SqlDataReader reader = command.ExecuteNonQuery();

reader.Close();
}

Comments

-2
string commandString = "SELECT * FROM myTable WHERE time BETWEEN '" + one_hour_ago + "' AND '" + current_time + "'";

EDIT: this is just what OP explicitly asked for, for a better (right) answer take a look at Jane Doe's...

EDIT 2: For all those ignoramuses that downvoted me, "one_hour_ago" and "current_time" are clearly not user entered strings (but his own DateTime vars), and can be in any case made completely foolproof with just a simple TryParse before using them (and that is all that sql parameters do too in that regard, there is no added magic to it). Now, string concatenating a sql cmd is wrong, but I didn't suggest it, I merely just corrected his own approach to it. And I could have warned him about sql injection, but seeing as how he had problems with a simple string operation, I just judged it to be the least of his problems (right now), and assumed that it would only confuse him further to no end.

6 Comments

-1 You should never suggest a string concatenation to build a Sql Text.
Steve might want to elaborate this opens you up to SQL injection attacks: en.wikipedia.org/wiki/SQL_injection
@Joris, you are right, but this question repeat itself from an age, also the OP should know that using a Parameter he could stop to worry to forget that data type prefix
@Steve: why are you trolling on my answer, if you don't like the question, take it up with the OP - I simply provided exactly what was asked for... if you wanted to "help", then next time do something actually useful, as Jane Doe did
I have all the rights to say that your proposed answer is not good, you could claim the contrary and the OP decides accepting or not while the community upvote or downvote. This is how this site works. Also I have answered to @joris not to you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.