调用存储过程

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. js函数(2)

    8.3函数的形参和实参 js中的函数并未指定函数形参的类型,函数调用也未对传入的实参值做任何类型的检查. 8.3.1函数的形参和实参 当调用函数时传入的实参比函数声明时指定的形参个数要少,剩下的参数都 ...

  2. luogu P1115 最大子段和 (dp)

    链接: https://www.luogu.org/problemnew/show/P1115 题面: 题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大. 输入输出格式 输入格式: 第一 ...

  3. Python+requests重定向和追踪

    Python+requests重定向和追踪 一.什么是重定向 重定向就是网络请求被重新定个方向转到了其它位置 二.为什么要做重定向 网页重定向的情况一般有:网站调整(如网页目录结构变化).网页地址改变 ...

  4. 获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据

    request中发送json数据用post方式发送Content-type用application/json;charset=utf-8方式发送的话,直接用springMVC的@RequestBody ...

  5. Spring4学习回顾之路11-AOP

    Srping的核心除了之前讲到的IOC/DI之外,还有一个AOP(Aspect Oriented Programming:面向切面编程):通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 ...

  6. python以不同方式打印输出九九乘法表

    参考:http://www.cnblogs.com/suiy-160428/p/5594389.htmlpython输出 9*9 乘法口诀表 矩形输出九九乘法表: for i in range(1,1 ...

  7. 【数学】Eddy Walker

    #include<bits/stdc++.h> using namespace std; typedef long long ll; ; ll qpow(ll a,ll b){ ll an ...

  8. Jersey 写restful接口时QueryParam ,FormParam 等的区别

    今天用jersey写接口,发现有一个post方法中没有得到参数,查了半天发现自己一不小心将@formparam写成了@queryparam,真是一个悲伤的故事.在这里把几个参数类型整理了一下放出来. ...

  9. Linux设备驱动中的软件架构思想

    目录 更新记录 一.Linux驱动的软件架构 1.1 出发点 1.2 分离思想 1.3 分层思想 二.platform设备驱动 2.1 platform设备 2.2 platform驱动 2.3 pl ...

  10. git 报错fatal: not a git repository (or any of the parent directories): .git

    产生原因:一般是没有初始化git本地版本管理仓库,所以无法执行git命令 解决方法:操作之前执行以下命令行:  git init 初始化git,即可解决: