c# - how to do inner join between two tables in sql server 2008r2 -
i have 2 tables:
questionbank table qustion answer1 answer2 answer3 answer4 correct_ans sub_name result table username name correctanswer totalquestions percentage result sub_name
i want perform inner join between these 2 result read questionbank table , store in result table how in sql server 2008.please help.
also have code c#:
sqlcommand cmd = new sqlcommand( @"select count(result.username) correct_ans result inner join questionbank on result.q_id = questionbank.q_id , result.user_ans = questionbank.correct_ans , result.username = " + username + " "); sqlcommand cmd1 = new sqlcommand( @"select count(q_id) totalquestions questionbank";);
what have fine (although r.username
should where
, not join
clause); really really should parameterize, though:
using(var cmd = new sqlcommand(@" select count(r.username) correct_ans result r inner join questionbank q on r.q_id = q.q_id , r.user_ans = q.correct_ans r.username = @username")) { cmd.parameters.addwithvalue("username", username)) // use it; since returns single count, can use: int correct = (int)cmd.executescalar(); //...etc }
other that: should fine. if aren't seeing matches, try query in ssms, making sure prepend like:
declare @username nvarchar(200) = 'fred';
(or whatever). when filtering on value username
, immediate thought "case sensitivity" - sql server can case-sensitive or case-insensitive: configured case-sensitive, 'fred'
not match 'fred'
or 'fred'
or 'fred'
.
Comments
Post a Comment