c# - Properly check if a record already exists in a database -
i have read other questions on this, not help part. trying check if file has been uploaded(filename sent table) before creating record , allowing them upload same file again.
i using code , keeps telling me every file new file, when utilize same file testing. should result in "exists". connection established using "this.master.conn" please no sqlcommand stuff.
i tried using wildcards in query.
private string sqlcheck(string filename) { string check = "select videourl trainingvideo2 videourl '" + filename +"' , status=1;"; object ob = this.master.conn.executesqlscalarcommand(check); string result; if (dbnull.value.equals(ob)) { result = "exists"; } else { result = "newfile"; } homecoming result; }
also, have better(more efficient) way of doing this?
trying rewrite in c#.
private function checkname(name string) int32 dim sql string = "select id company name '" & name & "' " dim ob object = conn.executesqlscalarcommand(sql) if isdbnull(ob) homecoming 0 else homecoming cint(ob) end if end function
there new , more innovative methods devised around simple "replace ` , " characters ..." sql injection prevention techniques. in case, if videourl happens varchar (and not nvarchar), using unicode character u+02bc
(url encoded = %ca%bc
) pass in quote character unicode string, bypass c# checks, sql server conveniently convert quote character in query. 1 illustration of why should not doing :).
in terms of check, prefer using top 1
allow sql server cutting potential table scan short. so, utilize query:
select top 1 somenonnullintcolumn trainingvideo2 videourl ... , status=1;
execute query executescalar. if result null
, record not exist.
c# asp.net sql-server
No comments:
Post a Comment