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 ...
随机推荐
- Django生成数据表时报错
Django生成数据表时报错 WARNINGS: ?: (mysql.W002) MySQL Strict Mode is not set for database connection 'defau ...
- centos7.4安装图形界面及报错处理
笔者实验环境:centos 7.4.1708,安装时默认最小化安装. 安装命令: yum -y update #如果不想升级内核版本可以参考笔者另一篇文章,关于update跟upgrade的区别yum ...
- 搭建 python 3.5+pycharm 2017.1.3+django 1.12.0 首次 将sqlite3 迁移到mysql
- JVM描述符标识字符含义
标识字符 含义 B byte C char D double F float I int J long S short Z boolean V void L 对象类型,如Ljava/lang/Obje ...
- -bash: /usr/librxec/grepconf.sh:Nosuch file or directory
最近修改/etc/profile文件时,不小心在后面添加了source /etc/profile,导致使用xshell登录远程linux的时候出现下面的信息, 一直无法进入linux,将profile ...
- EFcore的 基础理解<二> shadow 特性
接着上一篇.在MyEFTestContext 类中添加这个方法 protected override void OnModelCreating(ModelBuilder modelBuilder) { ...
- CentOS如何安装MySQL8.0、创建用户并授权的详细步骤
# 安装相关软件 yum install -y gcc gcc-c++ openssl openssl-devel ncurses ncurses-devel make cmake # 获取MySQL ...
- python读取ubuntu系统磁盘挂载情况
磁盘挂载 利用df -h 的命令 此功能主要实现了python 命令行执行函数进行解析df 返回的数据 代码如下 : # liunx 系统获取 磁盘挂载的情况 代码 #!/usr/bin/pyt ...
- Android项目笔记整理(1)
第二部分 工作项目中以及平时看视频.看书或者看博客时整理的个人觉得挺有用的笔记 1.Activity界面切换: if(条件1){ setContentView(R.layout.ma ...
- 9.SpringMVC注解式开发-处理器的请求映射规则的定义
1.对请求URI的命名空间的定义 @RequestMapping的value属性用于定义所匹配请求的URI.但对于注解在方法上和注解在类上, 其value 属性 所指定的URI,意义是不同的 一个@C ...