ASP.NET中使用SQL存储过程的方法
作者:风未起时 发布时间:2007-08-24 09:31:00
标签:存储过程,asp.net
用ASP.NET与SQL SERVER可是缘份最好了,稍大的程序一般第一先考虑的是SQL SERVER,只是一些很考虑经济的才使用ACCESS等了。用SQL SERVER,为了使数据库的效率更好,一般都会才取存储过程,因存储过程执行速度快,并且可以实现一些高级的查询等功能。比如传入一些数据参数,但执行的SQL过程可能不同等。
下面就来个例子,建立一新的角色,要求角色的名字不能重复,以下是一存储过程。
CREATE PROCEDURE sp_AccountRole_Create
@CategoryID int,
@RoleName nvarchar(10),
@Description nvarchar(50),
@RoleID int output
AS
DECLARE @Count int
-- 查找是否有相同名称的记录
Select @Count = Count(RoleID) FROM Account_Role WHERE
RoleName = @RoleName
IF @Count = 0
Insert INTO Account_Role
(CategoryID, RoleName, Description) valueS
(@CategoryID, @RoleName, @Description)
SET @RoleID = @@IDENTITY
RETURN 1
GO
执行存储过程的C#过程:
SqlConnection DbConnection = new SqlConnection(mConnectionString);
SqlCommand command = new SqlCommand( "sp_AccountRole_Create", DbConnection);
DbConnection.Open(connectString);
// 废置SqlCommand的属性为存储过程
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@CategoryID", SqlDbType.Int, 4);
command.Parameters.Add("@RoleName", SqlDbType.NVarChar, 10);
command.Parameters.Add("@Description", SqlDbType.NVarChar, 50);
command.Parameters.Add("@RoleID", SqlDbType.Int, 4);
// 返回值
command.Parameters.Add("Returnvalue",
SqlDbType.Int,
4, // Size
ParameterDirection.Returnvalue,
false, // is nullable
0, // byte precision
0, // byte scale
string.Empty,
DataRowVersion.Default,
null );
command.parameters["@CategoryID"].value = permission.CategoryID;
command.parameters["@RoleName"].value = permission.PermissionName;
command.parameters["@Description"].value = permission.Description;
// 可以返回新的ID值
command.parameters["@RoleID"].Direction = ParameterDirection.Output;
int rowsAffected = command.ExecuteNonQuery();
int result = command.parameters["Returnvalue"].value;
int newID = command.parameters["@RoleID"].value;
功能挺强的吧,可以得到三个值,分别是行影响值,存储过程返回值,新的ID值。
0
投稿
猜你喜欢
- 默认情况下,PyCharm中如果有无法错误或者不符合PEP8规范代码下面会有波浪线,语法错误波浪线为红色(如下图的第10行),不符合PEP8
- 本文实例讲述了Python列表操作。分享给大家供大家参考,具体如下:#coding=utf8'''''
- 今天在做一个老项目时,遇到一个需求,在javascript将url中的参数加密解密,从网上找发现了这段有用的代码:<SCRIPT LA
- 本次爬取网站为opgg,网址为:” http://www.op.gg/champion/statistics”由网站界面可以看出,右侧有英雄
- python3 最常用的三种装饰器语法总结1.简述语法装饰器也叫函数装饰器,主要作用是在不修改原来函数的代码情况下(函数本身不会被修改,执行
- dict是python中的常用数据结构,应该尽量掌握其使用方法字典是另一种可变容器模型,且可存储任意类型对象。字典的每个键值 key=>
- 本文实例为大家分享了ajax实现无刷新上传文件功能的具体代码,供大家参考,具体内容如下详细代码如下<!DOCTYPE HTML>
- 在网上搜索了半天,最简单的办法是在新的数据库中创建和原名字一样的数据库,然后把.frm 文件拷贝进去就OK了。 可是,有些时候这样不行,查询
- 这是第二天了,工作之余和女朋友一起学Python3,代码都是她敲的,有点辣眼睛,仅做参考。1.题目:输入“姓名”,输出“你好,姓名”有关安装
- 本文实例讲述了Python操作MongoDB数据库的方法。分享给大家供大家参考,具体如下:>>> import pymon
- 前言最近,我已经成功将我的个人网站从 Flask 迁移到 Django 了,最早接触 Django 的时候大概是在 4 年前,我记得那个时候
- 最近在做教师评教系统,有一个‘个人信息'页面中有个编辑修改邮箱的功能,本来想得很简单,结果进坑了,搞了好久才出来。我想实现的效果是点
- 在windows10系统下安装两个不同版本的的python解释器,在通常情况下编译执行文件都是没问题的,但是加载或下载包的时候pip的使用就
- 现在有一个横向的IFRAME,需要通过点击iframe外的一个图片来横向滚动iframe内的一个html页,但又不想让看见iframe的滚动
- php写入mysql出现中文乱码的解决办法是:在建立数据库连接之后,将该连接的编码方式改为中文。代码如下:$linkID=@mysql_co
- Goland是JetBrains公司出品的一款开发Golang的工具。由于本人是JetBrains公司产品的重度使用者,好多都作为主力开发工
- 一,前言今天做的东西,还算可以,修改了若干个bug,自己又写成功的写了几个bug。增加了一个功能——
- 前言:大概一年前写的,前段时间跑了下,发现还能用,就分享出来了供大家学习,代码的很多细节不太记得了,也尽力做了优化。因为毕竟是微博,反爬技术
- 前言标准的 Bloom Filter 是一种比较简单的数据结构,只支持插入和查找两种操作。在所要表达的集合是静态集合的时候,标准 Bloom
- 列表添加元素append(),extend(),insert(),+list区别回忆初学python的时候,对列表list添加元素时,对类表