c# - What dictates the datetime format of DB when ADO.Net runs query? -


i have simple test query run against sql server database.

select isdate('1' + '/' + '25' + '/' + '1993') 

when run against database

set dateformat dmy 

it correctly returns 0

when run against database

set dateformat mdy 

it correctly returns 1

now problem when i'm running query sqlcommand object in .net don't result expect.

the rub i'm setting thread's culture info , datetime format d/m/y. when thread running in d/m/y format , database set d/m/y still 1 query when run sqlcommand.

sample code: (note: quickquery wrapper method runs sqlcommand against selected database)

string query = $" select isdate('1' + '/' + '25' + '/' + '1993')"; var dt = quickquery(query);  return $"{dt.rows[0][0]}" + $"            {new datetime(1993, 1, 25)}"; 

the output of = "1 25/01/1993 00:00:00"

what determining sqlcommand running in context of m/d/y when actual database , current thread set d/m/y?

i have confirmed database setting

dbcc useroptions 

edit 1:

just quick note - appreciate input doing things better way, valuable. particular case don't pick context of problem sadly, fix it. have varchar column each value month, day, , year. can null, or text. have create date time comparisons actual datetime columns ability work datetime format on server , on running thread!

essentially: avoid problem completely. never talk dates strings. use parameters, defined , typed.

essentially (switching dapper syntax, since supports parameters):

 datetime date = ...  var dt = connection.querysingle<datetime>("select @date + 2", new {date}); 

the same goes other data - similar issues numbers , decimal separators , group separators, , way right is not learn format sql server wants: not use strings, pass parameterized values. it'll make harder accidentally fall sql injection vulnerabilities.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -