调用存储过程

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. 【转帖】Linux的NUMA机制

    Linux的NUMA机制 http://www.litrin.net/2014/06/18/linux的numa机制/ NUMA(Non-Uniform Memory Access)字面直译为“非一致 ...

  2. 对JSON.parse()中存在转义字符的解决以及js中替换函数replace()的认识

    在工作中,遇到对页面数据进行转存json格式数据后存储在数据库中.然而在显示数据时遇到无法显示json中的数据,产生的bug 问题抛出: 1.首先认识下,在JSON.parse()将后台传过来的字符串 ...

  3. java版MD5签名工具类

    package com.net.util; import java.security.MessageDigest; /** * MD5签名工具类 * @author zhangdi * */ publ ...

  4. Kubernetes---网络通讯模式笔记

    ⒈kubernetes网络通讯模式     Kubernetes的网络模型假定了所有Pod都在一个可以直接连通的扁平的网络空间中,这在GCE(Google Compute Engine)里面是现成的网 ...

  5. 【计算机网络】-传输层-Internet传输协议-UDP

    [计算机网络]-传输层-UDP 简介 Internet协议集支持一个无连接的传输协议,该协议称为用户数据报协议(UDP,UserDatagram Protocol) .UDP为应用程序提供了一-种无需 ...

  6. python爬取网页数据并存储到mysql数据库

    #python 3.5 from urllib.request import urlopen from urllib.request import urlretrieve from bs4 impor ...

  7. 第六章 ZYNQ-MIZ701 GPIO使用之MIO

      6.0 本章难度系数★★☆☆☆☆☆ 6.1 GPIO简介 Zynq7000系列芯片有54个MIO(multiuse I/O),它们分配在 GPIO 的Bank0 和Bank1隶属于PS部分,这些I ...

  8. go环境变量配置 (GOROOT和GOPATH)的区别和含义

    GOROOT就是go的安装路径 在~/.bash_profile中添加下面语句: GOROOT=/usr/local/go export GOROOT 当然, 要执行go命令和go工具, 就要配置go ...

  9. ggpubr进行“paper”组图合并,也许比PS,AI更简单

    本文转载自微信公众号 “生信补给站”,https://mp.weixin.qq.com/s/41iKTulTwGcY-dHtqqSnLA 多个图形进行组图展示,可以既展示一个“事情”的多个角度,也可以 ...

  10. 安装HAXM报错:failed to configure driver unknown error. failed to open driver 并提示数字签证不可用

    1.安装微软补丁:https://www.microsoft.com/en-us/download/confirmation.aspx?id=46148 2.重启后重新安装HAXM