Resource: http://support.microsoft.com/kb/306574
For more information, see the following topics in the Microsoft .NET Framework Software Development Kit (SDK) documentation:- Overview of ADO.NET
- ADO.NET DataSet
- Using .NET Data Providers to Access Data
- For more general information about ADO.NET or Visual Basic .NET, refer to the following MSDN newsgroups:
microsoft.public.dotnet.framework.adonet
(http://go.microsoft.com/fwlink/?linkid=5819)
microsoft.public.dotnet.languages.vb (http://go.microsoft.com/fwlink/?linkid=5820)
microsoft.public.dotnet.languages.vb (http://go.microsoft.com/fwlink/?linkid=5820)
- For more information, see the following book:
Wyke, R. Allen, and Sultan Rehman and Brad Leupen.
XML Programming (Core Reference)
(http://go.microsoft.com/fwlink/?LinkId=8394 ). Microsoft Press, 2001.
- For more information, see the following Microsoft Training & Certification course:
2389 Programming with ADO.NET
(http://www.microsoft.com/learning/syllabi/en-us/2389Bfinal.mspx)
- For additional information about how to perform this task by using Microsoft Active Server Pages, click the article number below to view the article in the Microsoft Knowledge Base:
300488
(http://support.microsoft.com/kb/300488/EN-US/) How To Run SQL Stored Procedures from an ASP Page
[@more@]
Create an ASP.NET Project and Add Controls
In this section, you create an ASP.NET project and build the basic user interface. Note that these steps use Microsoft Visual Basic .NET code. To create the project, follow these steps:1. | Click Start, point to Programs, point to Microsoft Visual Studio .NET, and then click Microsoft Visual Studio .NET. | |||||||||||||||
2. | On the Visual Studio .NET Start page, click New Project. | |||||||||||||||
3. | In the New Project dialog box, click Visual Basic Projects under Project Types, and then click ASP.NET Web Application under Templates. | |||||||||||||||
4. | In the Name box, type a name for your Web application, and then click OK. | |||||||||||||||
5. | Add the following server controls to the Web Form, and set the properties as they are listed in the table:
| |||||||||||||||
6. | Drag a DataGrid server control from the toolbox to the Web Form, and then set the Name property to GrdAuthors. | |||||||||||||||
7. | Right-click the grid, and then click Autoformat. | |||||||||||||||
8. | Click Professional 1 for the scheme, and then click OK. |
Create the GetAuthorsByLastName Stored Procedure
Use the following Transact-SQL code to create the GetAuthorsByLastName stored procedure:
Create Procedure GetAuthorsByLastName1 (@au_lname varchar(40), @RowCount int output)
as
select * from authors where au_lname like @au_lname;
/* @@ROWCOUNT returns the number of rows that are affected by the last statement. */
select @RowCount=@@ROWCOUNT
This code includes two parameters: @au_lname and @RowCount. The @au_lname parameter is an input parameter that obtains the search string to perform a "like" search in the Authors table. The @RowCount parameter is an output parameter that uses the @@ROWCOUNT variable to obtain the affected rows.
Create and Run the Stored Procedure
To access SQL Server databases, you must import the System.Data.SqlClient namespace, which provides new objects such as the SqlDataReader and the SqlDataAdapter objects. You can use SqlDataReader to read a forward-only stream of rows from a SQL Server database. DataAdapter represents a set of data commands and a database connection that you can use to fill the DataSet object and to update a SQL Server database.ADO.NET also introduces the DataSet object, which is a memory-resident representation of data that provides a consistent, relational programming model regardless of the data source. The code in this section uses all of these objects.
1. | Double-click the Web Form. |
2. | Add the following code to the Declaration section of your Web Form, which appears at the top of the Code window:
Imports System.Data |
3. | To make sure that the stored procedure exists and to create a new stored procedure, use a SqlCommand object with a SqlDataReader object. You can use SqlCommand to run any SQL commands against the database. Then call the ExecuteReader method of SqlCommand to return SqlDataReader, which contains matching rows for your query. Add the following code in the Page_Load event of the Web Form:
'Only run this code the first time the page is loaded. |
4. | Call the stored procedure in the Click event of the btnGetAuthors button, and then use the SqlDataAdapter object to run your stored procedure. You must create parameters for the stored procedure and append it to the Parameters collection of the SqlDataAdapter object. Add the following code after the Page_Load event:
Private Sub btnGetAuthors_Click(ByVal sender As System.Object, _ |
5. | In Solution Explorer, right-click the .aspx page, and then click Set as Start Page. |
6. | Save the project, and then click Start in Visual Studio .NET. Notice that the project is compiled and that the default page runs. |
7. | Type the author's last name in the text box, and then click Get Author. Notice that the stored procedure is called and that the returned rows populate the DataGrid. You can provide SQL Server-type search strings such as G%, which returns all the authors by last names that start with the letter "G." |
• | If you cannot connect to the database, make sure that the ConnectionString properly points to the server that is running SQL Server. | ||||
• | If you can connect to the database, but if you experience problems when you try to create the stored procedure, make sure that you have the correct permissions to create stored procedures in the database to which you are connecting. |
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/1697933/viewspace-893889/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/1697933/viewspace-893889/