调用存储过程

sql

 IF OBJECT_ID('RegionInsert') IS NULL
EXEC ('
--
-- Procedure which inserts a region record and returns the key
--
CREATE PROCEDURE RegionInsert(@RegionDescription NCHAR(50),
@RegionID INTEGER OUTPUT)AS
SET NOCOUNT OFF; SELECT @RegionID = MAX ( RegionID ) + 1
FROM Region ; INSERT INTO Region(RegionID, RegionDescription)
VALUES(@RegionID, @RegionDescription);') IF OBJECT_ID('RegionUpdate') IS NULL
EXEC ('
--
-- Procedure to update the description of a region
--
CREATE PROCEDURE RegionUpdate(@RegionID INTEGER,
@RegionDescription NCHAR(50))AS
SET NOCOUNT OFF; UPDATE Region
SET RegionDescription = @RegionDescription
WHERE RegionID = @RegionID;') IF OBJECT_ID('RegionDelete') IS NULL
EXEC ('
--
-- Procedure to delete a region
--
CREATE PROCEDURE RegionDelete (@RegionID INTEGER) AS
SET NOCOUNT OFF; DELETE FROM Region
WHERE RegionID = @RegionID;');

代码

 class Program
{
private static string constr = "server=.;database=northwnd;integrated security=sspi";
static void Main(string[] args)
{
using (SqlConnection con=new SqlConnection(constr))
{
con.Open();
InitialiseDatabase(con);
// Generate the update command
SqlCommand updateCommand = GenerateUpdateCommand(con); // Generate the delete command
SqlCommand deleteCommand = GenerateDeleteCommand(con); // And the insert command
SqlCommand insertCommand = GenerateInsertCommand(con); DumpRegions(con, "Regions prior to any stored procedure calls"); insertCommand.Parameters["@RegionDescription"].Value = "South West"; // Then execute the command
insertCommand.ExecuteNonQuery(); // And then get the value returned from the stored proc
int newRegionID = (int)insertCommand.Parameters["@RegionID"].Value; DumpRegions(con, "Regions after inserting 'South West'"); updateCommand.Parameters[].Value = newRegionID;
updateCommand.Parameters[].Value = "South Western England";
updateCommand.ExecuteNonQuery(); DumpRegions(con, "Regions after updating 'South West' to 'South Western England'"); // Delete the newly created record
deleteCommand.Parameters["@RegionID"].Value = newRegionID;
deleteCommand.ExecuteNonQuery(); DumpRegions(con, "Regions after deleting 'South Western England'"); con.Close();
}
} private static void InitialiseDatabase(SqlConnection conn)
{
SqlCommand cmd = new SqlCommand(Resource1.CreateSprocs, conn);
cmd.ExecuteNonQuery();
} private static SqlCommand GenerateUpdateCommand(SqlConnection con)
{
SqlCommand cmd = new SqlCommand("RegionUpdate", con); cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@RegionID", SqlDbType.Int, 0, "RegionID"));
cmd.Parameters.Add(new SqlParameter("@RegionDescription", SqlDbType.NChar, 50, "RegionDescription"));
cmd.UpdatedRowSource = UpdateRowSource.None;
return cmd;
}
private static SqlCommand GenerateInsertCommand(SqlConnection conn)
{
SqlCommand aCommand = new SqlCommand("RegionInsert", conn); aCommand.CommandType = CommandType.StoredProcedure;
aCommand.Parameters.Add(new SqlParameter("@RegionDescription", SqlDbType.NChar, 50, "RegionDescription"));
aCommand.Parameters.Add(new SqlParameter("@RegionID", SqlDbType.Int, 0, ParameterDirection.Output,
false, 0, 0, "RegionID", DataRowVersion.Default, null));
aCommand.UpdatedRowSource = UpdateRowSource.OutputParameters; return aCommand;
}
private static SqlCommand GenerateDeleteCommand(SqlConnection conn)
{
SqlCommand aCommand = new SqlCommand("RegionDelete", conn); aCommand.CommandType = CommandType.StoredProcedure;
aCommand.Parameters.Add(new SqlParameter("@RegionID", SqlDbType.Int, 0, "RegionID"));
aCommand.UpdatedRowSource = UpdateRowSource.None; return aCommand;
}
private static void DumpRegions(SqlConnection conn, string message)
{
SqlCommand aCommand = new SqlCommand("SELECT RegionID , RegionDescription From Region", conn); // Note the use of CommandBehaviour.KeyInfo.
// If this is not set, the default seems to be CommandBehavior.CloseConnection,
// which is an odd default if there ever was one. Oh well.
SqlDataReader aReader = aCommand.ExecuteReader(CommandBehavior.KeyInfo); Console.WriteLine(message); while (aReader.Read())
{
Console.WriteLine(" {0,-20} {1,-40}", aReader[], aReader[]);
} aReader.Close();
} }

C# ado.net 操作存储过程(二)的更多相关文章

  1. MFC ADO数据库操作

    MFC ADO数据库操作 - 延陵小明 - CSDN博客 http://blog.csdn.net/guoming0000/article/details/7280070/ 内容比较乱,作为草稿,对现 ...

  2. Asp.Net Core 2.0 项目实战(4)ADO.NET操作数据库封装、 EF Core操作及实例

    Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了 Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架 Asp.Ne ...

  3. C# 数据操作系列 - 2. ADO.NET操作

    0.前言 在上一篇中初略的介绍了一下SQL的基本写法,这一篇开始我们正式步入C#操作数据库的范围.通过这一系列的内容,我想大家能对于数据库交互有了一定的认识和基础.闲话不多说,先给大家介绍一个C#操作 ...

  4. 【翻译】MongoDB指南/CRUD操作(二)

    [原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关 ...

  5. VC与ADO数据库操作

    VC与ADO数据库操作 学研部的同志们,大家好! 想开一次学习会,实习时间冲突了,只好把文档发给大家看了.重点推荐李振龙的BMP读图教程! 尤其是大三GIS班的同志,注意了,可能实习用得上的! 一.A ...

  6. c# xml操作(二)

    c# xml操作(二) 此博文包含图片 (-- ::)转载▼ 标签: 杂谈 分类: c# 上次,我们介绍了增加和删除xml的一些操作,这次我们将介绍如何更改和读取xml特定节点.我们依然以上次的xml ...

  7. 使用C# 操作存储过程,执行sql语句通用类

    如何使用C# 操作存储过程,执行sql语句? 闲话不多说,直接上代码:     /// <summary>    /// Sql通用类    /// </summary>    ...

  8. ansible笔记(5):常用模块之文件操作(二)

    ansible笔记():常用模块之文件操作(二) 文件操作类模块 find模块 find模块可以帮助我们在远程主机中查找符合条件的文件,就像find命令一样. 此处我们介绍一些find模块的常用参数, ...

  9. 在nodeJS中操作文件系统(二)

    在nodeJS中操作文件系统(二)   1. 移动文件或目录 在fs模块中,可以使用rename方法移动文件或目录,使用方法如下:     fs.rename(oldPath,newPath,call ...

随机推荐

  1. 什么是 redis 的雪崩、穿透和击穿?

    缓存雪崩 对于系统 A,假设每天高峰期每秒 5000 个请求,本来缓存在高峰期可以扛住每秒 4000 个请求,但是缓存机器意外发生了全盘宕机.缓存挂了,此时 1 秒 5000 个请求全部落数据库,数据 ...

  2. Oracle的查询-分页查询

    --Oracle中的分页 --rownum行号:当我们做select操作时候 --每查询出一行记录,就在该行加上一个行号 --行号从1开始,一次递增,不能跳着走 ----emp表工资倒叙排列后,每页5 ...

  3. 2019.08.02 云从科技C++后台开发

    公司坐标:重庆 岗位:C++后台开发 面试时长:45分钟 主要问题记录: (1)手写代码 冒泡算法的实现: /**   * 冒泡排序:C++   *   * @author skywang   * @ ...

  4. ORC相关的库介绍和应用

    将图像翻译成文字一般被称为光学文字识别(Optical Character Recognition,OCR) OCR库:Pillow.Tesseract.NumPy Pillow Pillow可以对图 ...

  5. Spring实战(五)Spring中条件化地创建bean

    1.@Conditional 为生成bean设置条件 Spring 4中引入了一个新的注解---@Conditional,它用在有@Bean的方法上. 如果给定条件计算结果为true,Spring会创 ...

  6. IntelliJ IDEA热部署插件JRebel免费激活图文教程(持续更新)转载

    之前教了大家如何免费激活IDEA,大家学会了吗?今天再来教大家如何免费激活JRebel插件,实现真正的热部署,无论是改了代码片段还是配置文件,都可以做到不用重新启动就生效,这种酸爽,谁用谁知道! 这次 ...

  7. POJ 1860 汇率 SPFA

    题意 有多种汇币,汇币之间可以交换,这需要手续费,当你用100A币交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 ...

  8. SpringBoot事务隔离等级和传播行为

    一.开启事物管理 //import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBoo ...

  9. 服务器爆满:cannot create temp file for here-document: No space left on device

    1 概述 服务器的磁盘空间被占满导致TAB补全指令失效(TAB会创建临时文件) cannot create temp file for here-document: No space left on ...

  10. WindowsAPI使用详解——GetVersion|GetVersionEx 获取操作系统版本和名称

      Windows API 中有两个函数可以得到系统版本信息:GetVersion和GetVersionEx.      GetVersion这个函数曾经困扰了很多程序员,其本来设计的是在DWORD返 ...