C#运用存储过程新增一条记录并返回自动生成的ID

上传人:平*** 文档编号:10839858 上传时间:2017-10-10 格式:DOC 页数:6 大小:53.41KB
返回 下载 相关 举报
C#运用存储过程新增一条记录并返回自动生成的ID_第1页
第1页 / 共6页
C#运用存储过程新增一条记录并返回自动生成的ID_第2页
第2页 / 共6页
C#运用存储过程新增一条记录并返回自动生成的ID_第3页
第3页 / 共6页
C#运用存储过程新增一条记录并返回自动生成的ID_第4页
第4页 / 共6页
C#运用存储过程新增一条记录并返回自动生成的ID_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《C#运用存储过程新增一条记录并返回自动生成的ID》由会员分享,可在线阅读,更多相关《C#运用存储过程新增一条记录并返回自动生成的ID(6页珍藏版)》请在金锄头文库上搜索。

1、Author Liuker C#运用存储过程新增一条记录并返回自动生成的 IDC#运用存储过程新增一条记录并返回自动生成的 ID前言:1、存储过的好处:存储过程相对于其他的数据库访问方法有以下的优点:(1)重复使用。存储过程可以重复使用,从而可以减少数据库开发人员的工作量。(2)提高性能。存储过程在创建的时候就进行了编译,将来使用的时候不用再重新编译。一般的 SQL 语句每执行一次就需要编译一次,所以使用存储过程提高了效率。(3)减少网络流量。存储过程位于服务器上,调用的时候只需要传递存储过程的名称以及参数就可以了,因此降低了网络传输的数据量。(4)安全性。参数化的存储过程可以防止 SQL

2、注入式的攻击。2、存储过程知识拓展:(1)存储过程共分为 3 类:A.返回记录集的存储过程。其执行结果是一个记录集,例如:从数据库中检索出符合某一个或几个条件的记录B.返回数值的存储过程(也可以称为标量存储过程)。其执行完以后返回一个值,例如:在数据库中执行一个有返回值的函数或命令C.行为存储过程。用来实现数据库的某个功能,而没有返回值,例如:在数据库中的更新和删除操作(2)含有 return 的存储过程其返回值为 return 返回的那个值。(3)没有 return 的存储过程,不论执行结果有无记录集,其返回值是 0。(4)带输出参数的存储过程:假如有 return 则返回 return 返

3、回的那个值,假如要 select 输出参数,则出现输出参数的值,于有无 return 无关。3、需求:有的时候,我们可能需要得到由存储过程自动生成的 ID,那么下面我将详细介绍。一、数据设计1.数据表设计如下表(表名为 users):字段名 字段类型 是否为 null 字段默认值 备注nc_uid nchar(20) 用户 ID(Primary Key )nvc_username nvarchar(50) 用户名nvc_password nvarchar(50) 密码2.存储过程如下:if exists (select * from dbo.sysobjects where id = obje

4、ct_id(Nsp_users_add) and OBJECTPROPERTY(id, NIsProcedure) = 1)Author Liuker C#运用存储过程新增一条记录并返回自动生成的 IDdrop procedure sp_users_addCREATE PROCEDURE sp_users_Addnc_uid nchar(20) output, /ouput 表示该参数是可以输入输出的nvc_username nvarchar(50),nvc_password nvarchar(50)AS set nc_uid = replace(replace(replace(CONVER

5、T(nchar(20), getdate(), 120 ),-,), ,),:,)+DATENAME(YEAR,GETDATE()+CAST(FLOOR(RAND()*10) AS varchar)+CAST(FLOOR(RAND()*10) AS varchar)while exists(select * from users where nc_uid=nc_uid)beginset nc_uid = replace(replace(replace(CONVERT(nchar(20), getdate(), 120 ),-,), ,),:,)+CAST(FLOOR(RAND()*10) AS

6、 varchar)+DATENAME(YEAR,GETDATE()+CAST(FLOOR(RAND()*10) AS varchar)endbeginINSERT INTO users(nc_uid,nvc_username,nvc_password)VALUES(nc_uid,nvc_username,nvc_password)endreturn 1GO二、后台1.model 类using System;namespace Model/ / shuju:实体类(属性说明自动提取数据库字段的描述信息)/ Serializablepublic partial class usersModelpu

7、blic usersModel() #region ModelAuthor Liuker C#运用存储过程新增一条记录并返回自动生成的 IDprivate string _nc_uid;private string _nvc_username;private string _nvc_password;/ / 用户 id(Primary Key)/ public string nc_uidset _nc_uid = value; get return _nc_uid / / 用户名/ public string nvc_usernameset _nvc_username = value; ge

8、t return _nvc_username; / / 密码/ public string nvc_passwordset _nvc_password = value; get return _nvc_password; #endregion Model2.DAL 类using System;using System.Data;using System.Text;using System.Data.SqlClient;using DBUtility;using Model;Author Liuker C#运用存储过程新增一条记录并返回自动生成的 IDnamespace DAL/ / 数据访问

9、类:users/ public partial class usersDALpublic usersDAL()/ / 新增一条记录 执行存储过程/ / Model/ idpublic string AddByProcedure(usersModel model)string uid = null;IDataParameter parameters = new SqlParameter(nc_uid, SqlDbType.NChar,20),new SqlParameter(nvc_username, SqlDbType.NVarChar,50),new SqlParameter(nvc_pas

10、sword, SqlDbType.NVarChar,50);parameters0.Direction = ParameterDirection.Output;/注意这里parameters1.Value = model.nvc_username;parameters2.Value = model.nvc_password;uid = DbHelperSQL.RunProcedure(sp_users_Add , nc_uid, parameters).ToString();return uid;3.DbHelperSQL 类using System;using System.Collecti

11、ons.Generic;using System.Linq;using System.Text;using System.Data.SqlClient;Author Liuker C#运用存储过程新增一条记录并返回自动生成的 IDusing System.Data;using System.Collections;namespace DBUtility/ / 数据访问抽象基础类/ public abstract class DbHelperSQL/数据库连接字符串(web.config 来配置),多数据库可使用 DbHelperSQLP 来实现.public static string co

12、nnectionString = PubConstant.ConnectionString;public DbHelperSQL()/ /执行存储过程,返回 Output 输出参数值 / / 存储过程名 / 要返回值的参数名/ 存储过程参数 / stringpublic static object RunProcedure(string storedProcName, string output, IDataParameter paramenters)using (SqlConnection connection = new SqlConnection(connectionString)con

13、nection.Open();SqlCommand command = BuildQueryCommand(connection, storedProcName, paramenters);/记录条数command.ExecuteNonQuery(); /output 和具体的存储程参数对应object obj = command.Parametersoutput.Value.ToString(); if (Object.Equals(obj, null) | (Object.Equals(obj, System.DBNull.Value)return null;Author Liuker

14、C#运用存储过程新增一条记录并返回自动生成的 IDelsereturn obj;/ / 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)/ / 数据库连接 / 存储过程名 / 存储过程参数 / SqlCommandprivate static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter parameters)SqlCommand command = new SqlCommand(storedProcName, connection);command.CommandType = CommandType.StoredProcedure;foreach (SqlParameter parameter in parameters)if (parameter != null)/ 检查未分配值的输出参数,将其分配以 DBNull.Value.if (parameter.Direction = ParameterDirection.InputOutput | parameter.Direction = ParameterDirection.I

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 试题/考题 > 初中试题/考题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号