Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
144
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Chapter 8: Speaking SQL

Finally, with the new department and employee in place, the stored procedure selects the list of departments together with their employees:

-- List the departments together with their employees SELECT Departments.Department, Employees.Name

FROM Departments

INNER JOIN Employees ON Departments.DepartmentID = Employees.DepartmentID

For the purposes of this example, we’d prefer to keep the database tidy, which is why we’re deleting the new records at the end of the stored procedure. The department and employee IDs that we saved earlier come in very handy at this point: without them, we wouldn’t have any way to guarantee that we were deleting the right records!

--Delete the new employee DELETE FROM Employees

WHERE EmployeeID=@NewEmployeeID

--Delete the new department DELETE FROM Departments

WHERE DepartmentID=@NewDepartmentID

As you can see, a stored procedure is similar to a function in VB or C#: just like functions in VB or C# code, stored procedures can accept parameters, perform calculations based on those parameters, and return values. SQL also allows for some of the other programming constructs we’ve seen in this book, such as If statements, While loops, and so on, but advanced stored procedure programming is a little beyond the scope of this book.

Summary

Robust, reliable data access is crucial to the success of any application, and SQL meets those needs. As you have seen, SQL not only returns simple results from individual tables, but can produce complex data queries complete with filtering, sorting, expressions, and even nested statements.

In the latter part of this chapter, we learned how to group T-SQL statements and save them together as stored procedures. In Chapter 9, you’ll begin to use the knowledge you’ve gained about databases, and the language that connects those databases together, to create a real, working application.

330

9 ADO.NET

Through the preceding chapters, you’ve made major strides into the world of dynamic web development using ASP.NET. You’ve learned about interface development using web forms and web controls, you’ve learned about modeling and structuring your data within the framework of a database—you’ve even learned about the SQL language that’s used to access the data stored within your database. What you have not yet learned is how to access that data through your web applications.

The next step is to learn how to access a database using VB or C# code. This, of course, is the goal we’ve been aiming for from the beginning of our adventures in ASP.NET. The whole purpose of the data store is to support an application; in our case, that application is the Dorknozzle Intranet web site, the purpose of which is to offer users an easy-to-use interface to company data.

ADO.NET (ActiveX Data Objects .NET) is a modern Microsoft technology that permits us to access a relational database from an application’s code. With ADO.NET, we’ll be able to display lists of employees and departments, and allow users to add data to the data store, directly from the Dorknozzle application.

In this chapter, you’ll learn:

how to connect to your database using ADO.NET