随笔 - 11  文章 - 0  评论 - 0  阅读 - 2314

SQL 存储过程

文章来源: SQL Server 存储过程 - hoojo - 博客园 (cnblogs.com)

 

复制代码
---SQL Server 存储过程
-- 存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称、参数来执行。
--存储过程中可以包含逻辑控制语句和数据操纵语句,它可以接收参数、输出参数、返回单个或多个结果集以及返回值。
--由于存储过程在创建时就在数据库服务器上进行了编译并存储在数据库中,所以存储过程运行要比单个的SQL语句要快。同时由于在调用时只需用提供存储过程名和必要的参数信息,
所以在一定程度上也可以减少网络流量、减轻网络负担。
--存储过程的优点 -- A、 存储过程允许标准组件式编程 --存储过程创建后可以在程序中被多次调用执行,而不需要重新编写该存储过程的SQL语句。而且可以随时对存储过程SQL进行修改,但对应用程序源代码却毫无影响,从而极大的提高了程序的可移植性。 --B、 存储过程能够实现较快的执行速度 --如果某一操作包含大量的T-SQL语句代码,分别被多次执行,那么存储过程要比批处理的执行速度快得多。因为存储过程是预编译的,在首次运行一个存储过程时,查询优化器对其进行分析、优化,
并给出最优执行计划并存在系统表中的存储计划。而批处理的T-SQL语句每次运行都需要重新编译和优化,所以速度就要慢一些。
--C、 存储过程减轻网络流量 --对于同一个针对数据库对象的操作,如果这一操作所涉及到的T-SQL语句被组织成一存储过程,那么当在客户机上调用该存储过程时,网络中传递的只是该调用语句,否则将会是多条SQL语句。从而减轻了网络流量,降低了网络负载。 --D、 存储过程可被作为一种安全机制来充分利用 --系统管理员可以对执行的某一个存储过程进行权限限制,从而能够实现对某些数据访问的限制,避免非授权用户对数据的访问,保证数据的安全。 --系统存储过程 --系统存储过程是系统创建的存储过程,目的在于能够方便的从系统表中查询信息或完成与更新数据库表相关的管理任务或其他的系统管理任务。 --系统存储过程主要存储在master数据库中,以“sp”下划线开头的存储过程。尽管这些系统存储过程在master数据库中,但我们在其他数据库还是可以调用系统存储过程。有一些系统存储过程会在创建新的数据库的时候被自动创建在当前数据库中。 --常用系统存储过程有: exec sp_databases; --查看数据库 exec sp_tables; --查看表 exec sp_columns student;--查看列 exec sp_helpIndex student;--查看索引 exec sp_helpConstraint student;--约束 exec sp_stored_procedures; exec sp_helptext 'sp_stored_procedures';--查看存储过程创建、定义语句 exec sp_rename student, stuInfo;--修改表、索引、列的名称 exec sp_renamedb myTempDB, myDB;--更改数据库名称 exec sp_defaultdb 'master', 'myDB';--更改登录名的默认数据库 exec sp_helpdb;--数据库帮助,查询数据库信息 exec sp_helpdb master; -- 系统存储过程示例: --表重命名 exec sp_rename 'stu', 'stud'; --列重命名 exec sp_rename 'stud.name', 'sName', 'column'; exec sp_help 'stud'; --重命名索引 exec sp_rename N'student.idx_cid', N'idx_cidd', N'index'; exec sp_help 'student'; --查询所有存储过程 select * from sys.objects where type = 'P'; select * from sys.objects where type_desc like '%pro%' and name like 'sp%'; -- 用户自定义存储过程 -- 1、 创建语法 create proc | procedure pro_name [{@参数数据类型} [=默认值] [output], {@参数数据类型} [=默认值] [output], .... ] as SQL_statements -- 2、 创建不带参数存储过程 --创建存储过程 if (exists (select * from sys.objects where name = 'proc_get_student')) drop proc proc_get_student go create proc proc_get_student as select * from student; --调用、执行存储过程 exec proc_get_student; ----3、修改存储过程 alter proc proc_get_student as select * from student; --4、带参存储过程 if (object_id('proc_find_stu', 'P') is not null) drop proc proc_find_stu go create proc proc_find_stu(@startId int, @endId int) as select * from student where id between @startId and @endId go exec proc_find_stu 2, 4; --5、带通配符参数存储过程 if (object_id('proc_findStudentByName', 'P') is not null) drop proc proc_findStudentByName go create proc proc_findStudentByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%') as select * from student where name like @name and name like @nextName; go exec proc_findStudentByName; exec proc_findStudentByName '%o%', 't%'; --6、带输出参数存储过程 if (object_id('proc_getStudentRecord', 'P') is not null) drop proc proc_getStudentRecord go create proc proc_getStudentRecord( @id int, --默认输入参数 @name varchar(20) out, --输出参数 @age varchar(20) output--输入输出参数 ) as select @name = name, @age = age from student where id = @id and sex = @age; go -- declare @id int, @name varchar(20), @temp varchar(20); set @id = 7; set @temp = 1; exec proc_getStudentRecord @id, @name out, @temp output; select @name, @temp; print @name + '#' + @temp; -- 7、 不缓存存储过程 --WITH RECOMPILE 不缓存 if (object_id('proc_temp', 'P') is not null) drop proc proc_temp go create proc proc_temp with recompile as select * from student; go exec proc_temp; -- 8、 加密存储过程 --加密WITH ENCRYPTION if (object_id('proc_temp_encryption', 'P') is not null) drop proc proc_temp_encryption go create proc proc_temp_encryption with encryption as select * from student; go exec proc_temp_encryption; exec sp_helptext 'proc_temp'; exec sp_helptext 'proc_temp_encryption'; -- 9、 带游标参数存储过程 if (object_id('proc_cursor', 'P') is not null) drop proc proc_cursor go create proc proc_cursor @cur cursor varying output as set @cur = cursor forward_only static for select id, name, age from student; open @cur; go --调用 declare @exec_cur cursor; declare @id int, @name varchar(20), @age int; exec proc_cursor @cur = @exec_cur output;--调用存储过程 fetch next from @exec_cur into @id, @name, @age; while (@@fetch_status = 0) begin fetch next from @exec_cur into @id, @name, @age; print 'id: ' + convert(varchar, @id) + ', name: ' + @name + ', age: ' + convert(char, @age); end close @exec_cur; deallocate @exec_cur;--删除游标 -- 10、分页存储过程 ---存储过程、row_number完成分页 if (object_id('pro_page', 'P') is not null) drop proc proc_cursor go create proc pro_page @startIndex int, @endIndex int as select count(*) from product ; select * from ( select row_number() over(order by pid) as rowId, * from product ) temp where temp.rowId between @startIndex and @endIndex go --drop proc pro_page exec pro_page 1, 4 -- --分页存储过程 ---存储过程、row_number完成分页 if (object_id('pro_page', 'P') is not null) drop proc proc_cursor go create proc pro_page @startIndex int, @endIndex int as select count(*) from product ; select * from ( select row_number() over(order by pid) as rowId, * from product ) temp where temp.rowId between @startIndex and @endIndex go --drop proc pro_page exec pro_page 1, 4 -- --分页存储过程 if (object_id('pro_page', 'P') is not null) drop proc pro_stu go create procedure pro_stu( @pageIndex int, @pageSize int ) as declare @startRow int, @endRow int set @startRow = (@pageIndex - 1) * @pageSize +1 set @endRow = @startRow + @pageSize -1 select * from ( select *, row_number() over (order by id asc) as number from student ) t where t.number between @startRow and @endRow; exec pro_stu 2, 2; --分页存储示例row_number() over(order by id) as rownumber /rownumber between @startRow and @endRow if(OBJECT_ID('p_selectbypage','p') is not null) drop proc p_selectbypage go create proc p_selectbypage(@pageIndex int ,@pageSize int,@totalCount int out) as declare @startRow int declare @endRow int set @startRow=(@pageIndex-1)*@pageSize+1 set @endRow=@pageIndex*@pageSize select @totalCount= count(*) from Users select * from ( select *,row_number() over(order by userId) as rownumber from Users )temp where rownumber between @startRow and @endRow declare @totalRow int exec p_selectbypage 1,10,@totalRow out select @totalRow --分页存储示例 order by id offset (@pageIndex-1)*@pageSize rows fetch next @pageSize rows only //必须排序 if(OBJECT_ID('p_selectbypage2','p') is not null) drop proc p_selectbypage2 go create proc p_selectbypage2(@pageIndex int ,@pageSize int,@totalCount int out) as declare @skipRow int set @skipRow=(@pageIndex-1)*@pageSize select @totalCount= count(*) from Users select * from Users order by UserId offset @skipRow rows fetch next @pageSize rows only declare @totalRow int exec p_selectbypage2 1,10,@totalRow out select @totalRow ---无返回值存储过程 if(OBJECT_ID('cp_add_user','p') is not null) drop proc cp_add_user go create proc cp_add_user(@badge varchar(8),@password varchar(20),@email varchar(10),@name varchar(20),@cname nvarchar(20),@deptId varchar(50),@addBy varchar(8)) as INSERT INTO Users(Badge,Password,Name,CName,Email,DeptId,AddBy) VALUES(@badge,@password,@email,@name,@cname,@deptId,@addBy) --返回插入行ID if(OBJECT_ID('cp_add_user_return','p') is not null) drop proc cp_add_user_return go create proc cp_add_user_return(@badge varchar(8),@password varchar(20),@email varchar(10),@name varchar(20),@cname nvarchar(20),@deptId varchar(50),@addBy varchar(8),@userId varchar(50) out) as select @userId=NEWID() from Users INSERT INTO Users(UserId,Badge,Password,Name,CName,Email,DeptId,AddBy) VALUES(@userId,@badge,@password,@email,@name,@cname,@deptId,@addBy)
复制代码

 

ADO.NET 执行存储过程  

复制代码
using Microsoft.Data.SqlClient;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using xue.ado.net.test.Common;

namespace xue.ado.net.test.DAL
{
    public class BaseContext
    {
        private string connectionstring = string.Empty;

        public BaseContext() {
            connectionstring = AppConfiguration.GetSetting("ConnectionStrings:BaseContext");
        }


        public DataTable SqlSelectNoCommand(string sql, List<SqlParameter> sqlParameters = null)
        {
            DataTable dataTable = new DataTable();

            using (SqlConnection connection = new SqlConnection(connectionstring))
            {
                SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, connection);

                if (sqlParameters != null)
                {
                    dataAdapter.SelectCommand.Parameters.AddRange(sqlParameters.ToArray());
                }

                try
                {
                    connection.Open();
                    DataSet dataSet = new DataSet();
                    dataAdapter.Fill(dataSet);
                    dataTable = dataSet.Tables[0];
                }
                catch (Exception e)
                {
                    dataTable.TableName = "dt_error";
                }
                finally
                {
                    connection.Close();
                }

            }

            return dataTable;
        }

        public DataTable SqlSelect(string sql,List<SqlParameter> sqlParameters=null)
        {
            DataTable dataTable= new DataTable();

            using(SqlConnection connection = new SqlConnection(connectionstring))
            {
                SqlCommand sqlCommand= new SqlCommand(sql,connection);

                if (sqlParameters != null)
                {
                    sqlCommand.Parameters.AddRange(sqlParameters.ToArray());
                }

                try
                {
                    connection.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand);
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet);
                    dataTable = dataSet.Tables[0];
                }catch(Exception e)
                {
                    dataTable.TableName = "dt_error";
                }
                finally
                {
                    connection.Close();
                }

            }

            return dataTable;
        }

        public bool SqlUpdate(string sql,List<SqlParameter> sqlParameters=null)
        {
            using(SqlConnection conn = new SqlConnection(connectionstring))
            {
                SqlCommand sqlCommand= new SqlCommand(sql,conn);
                SqlTransaction sqlTransaction = conn.BeginTransaction();
                sqlCommand.Transaction = sqlTransaction;

                if (sqlParameters !=null)
                {
                    sqlCommand.Parameters.AddRange(sqlParameters.ToArray());
                }

                try
                {
                    conn.Open();
                  
                    int num = sqlCommand.ExecuteNonQuery();

                    if (num>0)
                    {
                        sqlTransaction.Commit();
                    }
                    else
                    {
                        sqlTransaction.Rollback();
                    }

                    return true;
                }
                catch (Exception e)
                {
                    sqlTransaction.Rollback();
                    return false;
                }
                finally { 
                    conn.Close(); 
                }
            }
        }

        /// <summary>
        /// 无返回值的存储过程
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="sqlParameters"></param>
        /// <returns></returns>
        public bool ExecProcdure(string procName,List<SqlParameter> sqlParameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionstring))
            {
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection= connection;
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = procName;

                sqlCommand.Parameters.AddRange(sqlParameters.ToArray());

                try
                {
                    connection.Open();
                    sqlCommand.ExecuteNonQuery();
                    return true;
                }
                catch (Exception)
                {

                    return false;
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="procName">存储过程名称</param>
        /// <param name="sqlParameters">输入参数</param>
        /// <param name="outSqlParameters">输入参数</param>
        /// <returns></returns>
        public bool ExecProcdure(string procName, List<SqlParameter> sqlParameters,List<SqlParameter> outSqlParameters,out Dictionary<string,string> dicValue)
        {
            dicValue= new Dictionary<string,string>();
            using (SqlConnection connection = new SqlConnection(connectionstring))
            {
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = connection;
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = procName;

                //添加输入参数
                sqlCommand.Parameters.AddRange(sqlParameters.ToArray());

                //添加输出参数/或输入输出参数
                sqlCommand.Parameters.AddRange(outSqlParameters.ToArray());

                try
                {
                    connection.Open();
                    sqlCommand.ExecuteNonQuery();

                    foreach (SqlParameter item in outSqlParameters)
                    {
                        dicValue.Add(item.ParameterName, Convert.ToString(sqlCommand.Parameters[item.ParameterName].Value));
                    }

                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        public DataTable ExecProResult(string procName, List<SqlParameter> sqlParameters)
        {
            DataTable dt = new DataTable();
            using (SqlConnection connection = new SqlConnection(connectionstring))
            {
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = connection;
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = procName;

                //添加输入参数
                sqlCommand.Parameters.AddRange(sqlParameters.ToArray());

                try
                {
                    connection.Open();
                    using(SqlDataReader reader = sqlCommand.ExecuteReader())
                    {
                        dt.Load(reader);//将结果加载到DataTable中
                    }

                    if (dt.Rows.Count>0)
                    {
                        dt.TableName = "dt_oks";
                    }
                    else
                    {
                        dt.TableName = "dt_ok";
                    }

                    return dt;
                }
                catch (Exception)
                {
                    dt.TableName = "dt_error";
                    return dt;
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        public DataTable ExecProcResult(string procName, List<SqlParameter> sqlParameters, List<SqlParameter> outSqlParameters, out Dictionary<string, string> dicValue)
        {
            dicValue = new Dictionary<string, string>();
            DataTable dataTable= new DataTable();
            using (SqlConnection connection = new SqlConnection(connectionstring))
            {
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = connection;
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = procName;

                //添加输入参数
                sqlCommand.Parameters.AddRange(sqlParameters.ToArray());

                //添加输出参数/或输入输出参数
                sqlCommand.Parameters.AddRange(outSqlParameters.ToArray());

                try
                {
                    connection.Open();
                    
                    using(SqlDataReader reader = sqlCommand.ExecuteReader())
                    {
                        dataTable.Load(reader);
                    }

                    foreach (SqlParameter item in outSqlParameters)
                    {
                        dicValue.Add(item.ParameterName, Convert.ToString(sqlCommand.Parameters[item.ParameterName].Value));
                    }

                    if (dataTable.Rows.Count > 0)
                    {
                        dataTable.TableName = "dt_oks";
                    }
                    else
                    {
                        dataTable.TableName = "dt_ok";
                    }

                    return dataTable;
                }
                catch (Exception)
                {
                    dataTable.TableName = "dt_error";
                    return dataTable;
                }
                finally
                {
                    connection.Close();
                }
            }
        }
    }
}
  
复制代码

 

使用示例

复制代码
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using xue.ado.net.test.DAL;
using xue.ado.net.test.Dto;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace xue.ado.net.test.BLL
{
    public class SystemService
    {
        private SystemDal systemDal = SystemDal.GetInstance;

        public DataTable GetUser()
        {
            return systemDal.GetUser();
        }

        public DataTable GetUsers()
        {
            return systemDal.GetUsers();
        }

        public bool AddUser(User user)
        {
            return systemDal.AddUser(user.Badge,user.Password,user.Name,user.CName,user.Email,user.DeptId,user.AddBy);
        }

        public bool AddUserInProc(User user)
        {
            List<SqlParameter> sqlParameters = new List<SqlParameter>() {
                new SqlParameter("@badge",user.Badge),
                new SqlParameter("@password",user.Password),
                new SqlParameter("@name",user.Name),
                new SqlParameter("@cname",user.CName),
                new SqlParameter("@email",user.Email),
                new SqlParameter("@deptId",user.DeptId),
                new SqlParameter("@addBy",user.AddBy)
            };

            return systemDal.ExecProcdure("cp_add_user", sqlParameters);
        }

        public bool AddUserReturnId(User user)
        {
            List<SqlParameter> sqlParameters = new List<SqlParameter>() {
                new SqlParameter("@badge",user.Badge),
                new SqlParameter("@password",user.Password),
                new SqlParameter("@name",user.Name),
                new SqlParameter("@cname",user.CName),
                new SqlParameter("@email",user.Email),
                new SqlParameter("@deptId",user.DeptId),
                new SqlParameter("@addBy",user.AddBy)
            };

            List<SqlParameter> outSqlParameters = new List<SqlParameter>();

            SqlParameter parameter = new SqlParameter("@userId", SqlDbType.VarChar, 50);
            parameter.Direction = ParameterDirection.Output;
            outSqlParameters.Add(parameter);

            Dictionary<string, string> keyValue;


            bool bln = systemDal.ExecProcdure("cp_add_user_return",sqlParameters,outSqlParameters,out keyValue);

            foreach (SqlParameter item in outSqlParameters)
            {
                string value = keyValue[item.ParameterName];
            }
            return bln;
        }

        public DataTable GetUserByPage(int pageIndex, int pageSize,out int totalCount)
        {
            totalCount = 0;
            List<SqlParameter> sqlParameters= new List<SqlParameter>() 
            { 
                new SqlParameter("@pageIndex",pageIndex),
                new SqlParameter("@pageSize",pageSize)
            };

            List<SqlParameter> outSqlParameters = new List<SqlParameter>();
            SqlParameter parameter = new SqlParameter("@totalCount", SqlDbType.Int);
            parameter.Direction = ParameterDirection.Output;
            outSqlParameters.Add(parameter);

            Dictionary<string, string> keyValue;
            DataTable dt = systemDal.ExecProcResult("p_selectbypage2", sqlParameters, outSqlParameters, out keyValue);

            if (keyValue.Keys.Contains("@totalCount"))
            {
                totalCount = int.Parse(keyValue["@totalCount"]);
            }

            return dt;
        }

        public bool UpdateUser(string cname,string userId)
        {
            return systemDal.UpdateUser(cname,userId);
        }

        public bool DeleteUser(string userId)
        {
            return systemDal.DeleteUser(userId);
        }
    }
}
复制代码

 

posted on   可乐加鸡翅  阅读(68)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示