sql server - Using results of a SqlDataReader in a SQL query -
i results of reader able used in where
clause in sql command. have tried put reader variable use not work. i've never used readers before not know how work. can give me idea of how result of reader there statement? thanks.
the code:
dim courseselectcom = new sqlcommand("select stuff((select ','+course_name course school= '" & schoolselect & "' xml path ('')), 1, 1, '')", connection) dim reader = courseselectcom.executereader() if reader.read() dim coursevar string coursevar = "%" & reader("course_name") & "%" using totalstudentcom = new sqlcommand("select count(student_id) student " & "course_name @course", connection) totalstudentcom.parameters.addwithvalue("@course", coursevar) dim result = totalstudentcom.executescalar() messagebox.show("students course = " & result.tostring) end using end if
there couple of problems here.
first should use parametrized query , not string concatenations building sql commands
second when datareader opened, connection object cannot serve command unless have specified substring multipleactiveresultsets=true;
in connection string
dim courseselectcom = new sqlcommand("select course_name course school= @school", connection) courseselectcom.parameters.addwithvalue("@school", schoolselect) dim reader = courseselectcom.executereader() if reader.read() dim coursevar string coursevar = "%" & reader("course_name") & "%" using totalstudentcom = new sqlcommand("select count(student_id) student " & _ "course_name @course", connection) totalstudentcom.parameters.addwithvalue("@course", coursevar) dim result = totalstudentcom.executescalar() messagebox.show("students course = " & result.tostring) end using end if
remember works if mars enabled
Comments
Post a Comment