C# ado.net 操作存储过程(二)
调用存储过程
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 操作存储过程(二)的更多相关文章
- MFC ADO数据库操作
MFC ADO数据库操作 - 延陵小明 - CSDN博客 http://blog.csdn.net/guoming0000/article/details/7280070/ 内容比较乱,作为草稿,对现 ...
- 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 ...
- C# 数据操作系列 - 2. ADO.NET操作
0.前言 在上一篇中初略的介绍了一下SQL的基本写法,这一篇开始我们正式步入C#操作数据库的范围.通过这一系列的内容,我想大家能对于数据库交互有了一定的认识和基础.闲话不多说,先给大家介绍一个C#操作 ...
- 【翻译】MongoDB指南/CRUD操作(二)
[原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关 ...
- VC与ADO数据库操作
VC与ADO数据库操作 学研部的同志们,大家好! 想开一次学习会,实习时间冲突了,只好把文档发给大家看了.重点推荐李振龙的BMP读图教程! 尤其是大三GIS班的同志,注意了,可能实习用得上的! 一.A ...
- c# xml操作(二)
c# xml操作(二) 此博文包含图片 (-- ::)转载▼ 标签: 杂谈 分类: c# 上次,我们介绍了增加和删除xml的一些操作,这次我们将介绍如何更改和读取xml特定节点.我们依然以上次的xml ...
- 使用C# 操作存储过程,执行sql语句通用类
如何使用C# 操作存储过程,执行sql语句? 闲话不多说,直接上代码: /// <summary> /// Sql通用类 /// </summary> ...
- ansible笔记(5):常用模块之文件操作(二)
ansible笔记():常用模块之文件操作(二) 文件操作类模块 find模块 find模块可以帮助我们在远程主机中查找符合条件的文件,就像find命令一样. 此处我们介绍一些find模块的常用参数, ...
- 在nodeJS中操作文件系统(二)
在nodeJS中操作文件系统(二) 1. 移动文件或目录 在fs模块中,可以使用rename方法移动文件或目录,使用方法如下: fs.rename(oldPath,newPath,call ...
随机推荐
- 【转】GMM与K-means聚类效果实战
原地址: GMM与K-means聚类效果实战 备注 分析软件:python 数据已经分享在百度云:客户年消费数据 密码:lehv 该份数据中包含客户id和客户6种商品的年消费额,共有440个样本 正文 ...
- Design Compressed String Iterator
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- #【Python】【基础知识】【内置对象常用方法】
数字 数字的常用方法: >>> dir(int) ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class ...
- pymysql连接和操作Mysql数据库
pymysql 一.概要 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库, 二.PyMySQL 安装 pip install pymysql 三.操作流程 创建c ...
- HDU-6170 Two strings
http://acm.hdu.edu.cn/showproblem.php?pid=6170 . 匹配任意字符,x* 匹配任意长度的 x (x 为任意字符,长度可以为 0 ) 正则表达式 #inclu ...
- ubuntu14中配置tomcat8
在ubuntu14.04中配置tomcat8. 1.下载tomcat 地址:http://tomcat.apache.org/download-80.cgi ubuntu可以下载tar.gz类型的或者 ...
- python学习-9 pycharm的安装
1.python 开发IDE : pycharm.eclipse等等 安装: 百度搜索pycharm 就可以了,去官网下载专业版.(百度有各种破解方法) #不要 ...
- 分享一些JVM常见的面试题(转)
出处: 分享一些JVM常见的面试题 前言: 在遨游了一番 Java Web 的世界之后,发现了自己的一些缺失,所以就着一篇深度好文:知名互联网公司校招 Java 开发岗面试知识点解析 ,来好好的对 ...
- 第十一章 ZYNQ-MIZ702 DDR3 PS读写操作方案
本编文章的目的主要用简明的方法在纯PS里对DDR3进行读写. 本文所使用的开发板是Miz702 PC 开发环境版本:Vivado 2015.4 Xilinx SDK 2015.4 11.0本章难度 ...
- 【Trie】Secret Message 秘密信息
[题目链接]: https://loj.ac/problem/10054 [题意] 我认为这个题目最难的是题意: 其实分了两种情况: 1.如果当前文本串匹配不完,那么答案的是:匹配过程中遇到的模式串结 ...