C# Cookbook (Executing Stored Procedures)

 Executing Stored Procedures

using
System;
using System.Data;
using System.Data.SqlClient;

class StoredProcDemo
{
static void Main()
{
StoredProcDemo spd = new StoredProcDemo();

// run a simple stored procedure
spd.RunStoredProc();

// run a stored procedure that takes a parameter
spd.RunStoredProcParams();
}

// run a simple stored procedure
public void RunStoredProc()
{
SqlConnection conn = null;
SqlDataReader rdr  = null;

Console.WriteLine("\nTop 10 Most Expensive Products:\n");

try
{
// create and open a connection object
conn = new
SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");
conn.Open();

// 1.  create a command object identifying
//     the stored procedure
SqlCommand cmd  = new SqlCommand(
"Ten Most Expensive Products", conn);

// 2. set the command object so it knows
//    to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;

// execute the command
rdr = cmd.ExecuteReader();

// iterate through results, printing each to console
while (rdr.Read())
{
Console.WriteLine(
"Product: {0,-25} Price: ${1,6:####.00}",
rdr["TenMostExpensiveProducts"],
rdr["UnitPrice"]);
}
}
finally
{
if (conn != null)
{
conn.Close();
}
if (rdr != null)
{
rdr.Close();
}
}
}

// run a stored procedure that takes a parameter
public void RunStoredProcParams()
{
SqlConnection conn = null;
SqlDataReader rdr  = null;

// typically obtained from user
// input, but we take a short cut
string custId = "FURIB";

Console.WriteLine("\nCustomer Order History:\n");

try
{
// create and open a connection object
conn = new
SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");
conn.Open();

// 1.  create a command object identifying
//     the stored procedure
SqlCommand cmd  = new SqlCommand(
"CustOrderHist", conn);

// 2. set the command object so it knows
//    to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;

// 3. add parameter to command, which
//    will be passed to the stored procedure
cmd.Parameters.Add(
new SqlParameter("@CustomerID", custId));

// execute the command
rdr = cmd.ExecuteReader();

// iterate through results, printing each to console
while (rdr.Read())
{
Console.WriteLine(
"Product: {0,-35} Total: {1,2}",
rdr["ProductName"],
rdr["Total"]);
}
}
finally
{
if (conn != null)
{
conn.Close();
}
if (rdr != null)
{
rdr.Close();
}
}
}
}

转载于:https://www.cnblogs.com/yangbin990/archive/2005/09/13/236279.html

C# 7 and .NET Core Cookbook by Dirk Strauss English | 25 Apr. 2017 | ASIN: B01N6QN9ZD | 628 Pages | AZW3 | 15.51 MB Key Features Easy-to-follow recipes to get you up-and-running with the new features of C# 7 and .NET Core 1.1 Practical solutions to assist you with microservices and serverless computing in C# Explore the new Visual Studio environment and write more secure code in it Book Description C# has recently been open-sourced and C# 7 comes with a host of new features for building powerful, cross-platform applications. This book will be your solution to some common programming problems that you come across with C# and will also help you get started with .NET Core 1.1. Through a recipe-based approach, this book will help you overcome common programming challenges and get your applications ready to face the modern world. We start by running you through new features in C# 7, such as tuples, pattern matching, and so on, giving you hands-on experience with them. Moving forward, you will work with generics and the OOP features in C#. You will then move on to more advanced topics, such as reactive extensions, Regex, code analyzers, and asynchronous programming. This book will also cover new, cross-platform .NET Core 1.1 features and teach you how to utilize .NET Core on macOS. Then, we will explore microservices as well as serverless computing and how these benefit modern developers. Finally, you will learn what you can do with Visual Studio 2017 to put mobile application development across multiple platforms within the reach of any developer. What you will learn Writing better and less code to achieve the same result as in previous versions of C# Working with analyzers in Visual Studio Working with files, streams, and serialization Writing high-performant code in C# and understanding multi-threading Demystifying the Rx library using Reactive extensions Exploring .Net Core 1.1 and ASP.NET MVC Securing your applications and learning new debugging techniques De
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值