Wednesday, July 17, 2013
A lot of times you want to call a stored procedure quickly in the code to access the stored procedure. You don't want to go through the trouble of dragging an SqlDataSource control in the design view just to use the stored procedure.
Here is how you would call the "Top Ten Most Expensive Products" stored procedure in the Northwind database.
1. First you need the namespaces
2. Then get the Northwind connection string value from the Web.config file
3. Now call the stored procedure and output the result from the SqlDataReader
In the above code the ExecuteScalar() method executes a call to the stored procedure called "GetProductsAvgPrice" which uses the aggregate function AVG() to get the average products price. The only thing you have to watch out for is that you have to convert the type to the appropriate type if you want to use it. Since currency is of type decimal you want to use the result into a decimal value because the ExecuteScalar() method returns an object type.
Here is how you would call the "Top Ten Most Expensive Products" stored procedure in the Northwind database.
1. First you need the namespaces
using System.Web.Configuration; using System.Data.SqlClient; using System.Data;
2. Then get the Northwind connection string value from the Web.config file
string connectString = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"]. ConnectionString;
3. Now call the stored procedure and output the result from the SqlDataReader
using (SqlConnection conn = new SqlConnection(connectString)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "GetProductsAvgPrice"; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = conn; Response.Write("Products Average Price is $" + (decimal)cmd.ExecuteScalar()); }
In the above code the ExecuteScalar() method executes a call to the stored procedure called "GetProductsAvgPrice" which uses the aggregate function AVG() to get the average products price. The only thing you have to watch out for is that you have to convert the type to the appropriate type if you want to use it. Since currency is of type decimal you want to use the result into a decimal value because the ExecuteScalar() method returns an object type.
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
Mua vé máy bay tại Aivivu, tham khảo
ReplyDeleteVe may bay di My
giá vé máy bay từ mỹ về việt nam
bao giờ có chuyến bay từ đức về việt nam
thông tin chuyến bay từ nga về việt nam
chuyến bay từ anh về việt nam
mua vé máy bay giá rẻ từ pháp về việt nam
chuyen bay chuyen gia trung quoc
Aivivu đại lý vé máy bay, tham khảo
ReplyDeletevé máy bay đi Mỹ giá rẻ 2021
vé máy bay từ mỹ về việt nam 2021
từ nhật bản về việt nam bao nhiêu tiếng
vé máy bay từ đức về việt nam giá rẻ
giá vé máy bay từ canada về Việt Nam
mở lại đường bay việt nam - hàn quốc
Chi phí cho chuyên gia nước ngoài
Đặt vé máy bay tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ giá bao nhiêu
giá vé về việt nam
tin tức các chuyến bay từ nhật về việt nam
đặt vé máy bay từ đức về việt nam
giá vé máy bay từ Vancouver về việt nam
gia ve may bay tu han quoc ve viet nam
chuyen bay chuyen gia trung quoc
The content was really very interesting.
ReplyDeleteHope more people will read this article
ReplyDeleteReally enjoyed reading your blog.
ReplyDelete