Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
08) 1
1. DownloadtheNorthwinddatabaseinstallationscript(onMoodle),openitintheMicrosoftSQLServer
ManagementStudio(File,Open,File)andexecuteit(F5)tocreatetheNorthwindDatabase.
2. InMicrosoftVisualStudio2012:
(a) establishadataconnectiontotheNorthwinddatabase
i. opentheServerExplorer(View,ServerExplorer)
ii. rightclickonDataConnectionsandselectAddConnection
iii. typeintheservername(virtualmachine:studentPC),selectNorthwindfromtheSelectionor
enteradatabasenamepulldownmenu,thenclickOK.
(b) createaWindowsFormsApplicationprojectandnameitDisplayEmpTable
(c) inthesolutionexploerer,renameForm1.cstoDisplayEmployeeTable.cs(rightclick,Rename,enter
andclickonYES)
(d) inDisplayEmployeeTable.cs[Design],clickontheform,thengotoPropertiesandchangethetextto
DisplayEmployeeTable
(e) generatetheLINQtoSQLclasses
i. intheSolutionExplorer,
A. rightclickontheprojectname(DisplayEmpTable),thenselectAdd,NewItem
B. selectLINQtoSQLClasses
C. changethenametoNorthwind.dbml
D. clickonAdd
ii. intheServerExplorer,
A. expandstudentpc.Northwind.dbo,thenexpandTables
B. dragEmployeestoNorthwind.dbmlintheeditor(knownastheObjectRelationalDesigner)
iii. saveall
(f) lookoverthegeneratedfilesandreferencesintheSolutionExplorer
TutorialonQueryinganMSSqlDatabaseTablewithLINQ(updated2013.11.08) 2
(g) addtheEmployeeLINQtoSQLclassasadatasource
i. selectProject,AddNewDataSource
ii. selectObjectandclickNext
iii. expandDisplayEmpTabletwice(drilldownnamesarethesame),thenselectEmployeeandclick
Finish
iv. thenewlycreatedEmployeeclasscanbeexaminedbyopeningtheDataSourceswindow(very
leftofVisualStudio)
(h) createtheGUI
i. switchtheeditorbacktoDisplayEmployeeTable.cs[Design]
ii. inDataSources,Employeewillnowbeadropdownmenu;clickonitandselectDataGridView
iii. dragEmployeefromDataSourcestotheforminthedesigneditor
iv. tofillthewindow,clickonthegridthengotothePropertyeditorandchangethevalueofDock
toFill
v. noticetheBindingNavigatortoolbaratthetopofthewindow
(i) connecttheNorthwindDataContexttotheemployeeBindingSource
i. createtheform'sloadhandler
A. doubleclickontheform'stitlebar
B. addthisprivatedatamembertoclassDisplayEmployeeTable
privateNorthwindDataContextdatabase
=newNorthwindDataContext();
C. addtheLINQquerytothemethodDisplayEmployeeTable_Load
employeeBindingSource.DataSource=
fromemployeeindatabase.Employees
orderbyemployee.EmployeeID
selectemployee;
TutorialonQueryinganMSSqlDatabaseTablewithLINQ(updated2013.11.08) 3
ii. addthecodetosavechangesbacktothedatabase
A. backinDisplayEmployeeTable.cs[Design],rightclickonthesaveicon(thefloppydisk)and
selectEnabled
B. doubleclickonthesaveiconandaddthiscodetothemethod
employeeBindingNavigatorSaveItem_Click
Validate();
employeeBindingSource.EndEdit();
database.SubmitChanges();
3. runtheproject,thenviewandmakechanges