Peter Hon
Peter Hon

Reputation: 27

passing string value to sql server

I am using SQL to retrive data from SQL-Server in C#, like below:

var cmd = new SqlCommand(
    "select *, replace(trucknocn, char(10), '') trucknocn1 from deliverylist  where deliverydate=@scantime", sqlCon);             
cmd.Parameters.Add("@scantime", SqlDbType.VarChar, 10).Value
    = Calendar1.SelectedDate.ToString("yyyy-MM-dd");            

da.SelectCommand = cmd;

My problem is that am I correct in doing this to avoid regional setting problems for my client application machine. Anything that I can do to totally avoid the regional setting problem?

Thanks

Upvotes: 1

Views: 331

Answers (2)

Mayeed
Mayeed

Reputation: 800

If deliverydate is varchar type then I did not find any problem in your code,

But if deliverydate is datetime then change the code like following -

your's

cmd.Parameters.Add("@scantime", SqlDbType.VarChar, 10).Value = Calendar1.SelectedDate.ToString("yyyy-MM-dd");

New Code:

cmd.Parameters.Add("@scantime", SqlDbType.DateTime).Value = Calendar1.SelectedDate;

Upvotes: 0

Cyril Gandon
Cyril Gandon

Reputation: 17048

Is deliverydate a varchar? I don't think so.
You can just add the parameter as DateTime:

cmd.Parameters.Add("@scantime", SqlDbType.DateTime).Value = Calendar1.SelectedDate;

Then all the regional setting problem are gone, since there is no string->Date->string conversion, and the comparaison are made base on the real date value, not just a string representation.

Upvotes: 5

Related Questions