Home > Microsoft .Net Development Tips > Microsoft SQL Server > Calling a stored procedure using ADO.NET
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

MICROSOFT SQL SERVER

Calling a stored procedure using ADO.NET


Kunal Cheda
11.26.2002
Rating: -2.67- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Let other users know how useful this tip is by rating it below. Got a tip or code of your own you'd like to share? Submit it here!


A stored procedure is code that is to be executed on the server in one single stroke. Using stored procedures introduces speed and momentum to programs. The example below executes a stored procedure on the SQL Server database. After understanding this example, you can try calling a Oracle stored procedure as well.

I have used ADO namespace to connect to the SQL Server database. If you want to use SQL namespace, simply change ADO with SQL in:

thecodeandalsoremembertochangethestr="Server=LocalHost;uid=sa;pwd=;database=test"

Steps to run this example:

  1. Create a database named Test in SQL Server.

  2. Create a table named Table1, add fields and some data to the table.

  3. Create a stored procedure called Proc1. The Syntax is CREATE PROCEDURE Proc1 AS Select * from Table1 GO.
  4. Copy the StoredProc.aspx in the wwwroot folder.
  5. Run http://localhost/StoredProc.aspx in the browser.

Here, the stored procedure returns a set of records that will be displayed in the grid when the page load event handler gets called.

StoredProc.aspx


<%@ import namespace="system.data"%>
<%@ import namespace="system.data.ado"%>
<html>
<head>
<script language="vb" runat="server">
              sub page_load(sender as object, e as eventargs)
                         dim con as adoconnection
                         dim cmd as adodatasetcommand 
                         dim ds as new dataset
                         dim str as string
                         dim sql as string

                         str="Provider=SQLOLEDB.1;Persist Security
Info=False;User ID=sa;Initial Catalog=Test;Data Source=LocalHost;"

                        sql="Proc1"
                        con= new adoconnection(str)
                        cmd= new adodatasetcommand(sql,con)

                    cmd.SelectCommand.CommandType= CommandType.StoredProcedure  
                        cmd.filldataset(ds,"Proc1") 

                       datagrid1.datasource=ds.tables("proc1").DefaultView
                       datagrid1.databind()
             end sub
</script>
</head>
<asp:datagrid id="datagrid1" runat="server"/>
</html>


Source: DotNetExtreme.com

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
.NET Developer
How to speed up Visual Studio 2008's slow WPF designer
How to list fonts in WPF using markup extensions and data templates
How to convince management to buy Microsoft Visual Studio 2008
Microsoft webcast series previews new Visual Studio 2010 features
New features in Windows 7 bring new UI considerations for developers
User Account Control (UAC): How to develop code for standard users
Introduction to Vista's user account control (UAC) for developers
Visual Studio 2008 code metrics tools will help keep code clean
Downloadable MSDN library released for VS 2008 SP 1
Widgetbox provides good example of how to use Facebook Developer Toolkit

Microsoft SQL Server
Using the Visual Studio 2005 DataSet Designer to build a data access layer
The CTE, the hierarchical query and SQL Server 2005
SQL Server 2005 recursive functions and the CTE
Choose the right .NET data provider, optimize application performance
The fallacy of the data layer -- or, a new architectural model for software designs
Book excerpt: ADO.NET and SQL Server 2005
Addressing common SQL Server questions
Book Excerpt: The .NET Framework and SQL Server 2005
WinForms development using SQL Server 2005 and Visual Basic 2005
Configuring ASP.NET 2.0 apps to SQL Server 2005 databases

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts