Don't.
Do the random part in C# - it's a lot better at it - and let SQL handle what it's god at: storing the data before (classes and how long each needs per period, and per week) and after (when each class is going to start, the room it's in, and how many periods it occupies) you have generated the actual timetable.
Do bear in mind that this can be a horribly complex task if you are doing it properly: you will have many lessons going on simultaneously, and you can't have a student that is doing both subjects in two places at the one time! Generally, random allocation won't work: you need some much better algorithms!
This[
^] may give you some pointers! :laugh: