An error with an sql string (c#) -
i have written following script (in c#):
string sqlcommand = "select * " + "from tblmatches " + "where matchplayernick =" + comboboxplayer.text + " " + "order matchname "; when run program, this: "data type mismatch in criteria experssion". datatype of matchplayer is, of course, "text".
what's wrong script then?
thanks!
string sqlcommand = "select * " + "from tblmatches " + "where matchplayernick ='" + comboboxplayer.text + "' " + "order matchname "; but query above vulnerable sql injection. can prevented if parameterized values using command object , parameters.
try code snippet:
string content = comboboxplayer.text; string connstr = "connection string here"; string sqlcommand = @"select * tblmatches matchplayernick = @content order matchname"; using (sqlconnection conn = new sqlconnection(connstr)) { using(sqlcommand comm = new sqlcommand()) { comm.connection = conn; comm.commandtext = sqlstatement; comm.commandtype = commandtype.text; comm.parameters.addwithvalue("@content", content); try { conn.open(); // other codes here } catch(sqlexception e) { // exception // not hide // e.message.tostring() } } } for proper coding
- use
usingstatement propr object disposal - use
try-catchblock handle objects
Comments
Post a Comment