sql生成(c#调用存储过程返回数据表)代码的存储过程

简介: GO /****** 对象: StoredProcedure [dbo].[pro_GenerateServiceFunction] 脚本日期: 08/04/2012 11:26:43 ******/ IF EXISTS (SELECT * FROM sys.
GO
/****** 对象:  StoredProcedure [dbo].[pro_GenerateServiceFunction]    脚本日期: 08/04/2012 11:26:43 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pro_GenerateServiceFunction]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pro_GenerateServiceFunction]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*****************************************************
** DECRIPTION: 生成(c#调用存储过程返回数据表)代码的存储过程
** VERSION      AUTH          DATE          Defect No			DESC
** --------  ------------  ------------  -----------------   ------------------------------
** V000.0.1    pukuimin     08/04/2012							新建程序
** --------  ------------  ------------  -----------------   -------------------------------
*******************************************************/
CREATE procedure [dbo].[pro_GenerateServiceFunction](
					@ProName NVARCHAR(200), ---存储过程名
					@TableName NVARCHAR(200) ---表名
)
as
begin
DECLARE @SqlParams     VARCHAR(8000)  --生成存储过程参数
DECLARE @ParamValue     VARCHAR(8000) --参数赋值
declare @tempProperty    varchar(200)--临时字段
declare @DATA_TYPE    varchar(200)--临时数据类型
declare @ParamCount int --参数计数器
declare @opr_typstr varchar(20) --
SELECT @SqlParams='',@tempProperty='',@DATA_TYPE='',@ParamCount=0,@ParamValue='',@opr_typstr=''

if isnull(@ProName,'')='' or isnull(@TableName,'')=''
begin
	print '存储过程名或表名不能为空!'
	return 0
end 

 if exists (select * from sys.all_parameters where object_id = object_id(@ProName))  
 begin  
	select 
	@opr_typstr=case when [name]='@opr_typ' and @opr_typstr='' then 'int Opr_typ ,' else @opr_typstr end,
	@DATA_TYPE=type_name(user_type_id),  --sql类型
	@tempProperty=dbo.fun_get_UpperFirst(replace([name],'@','')), --参数
	@SqlParams=@SqlParams+dbo.fun_get_tabspace(3)+'new  SqlParameter("'+[name]+'",'+
	(CASE
		WHEN @DATA_TYPE='NVARCHAR' OR @DATA_TYPE='VARCHAR' OR @DATA_TYPE='CHAR'OR @DATA_TYPE='NCHAR' or @DATA_TYPE='numeric'
		THEN dbo.[fun_get_cssqlpdt_by_sqldt](@DATA_TYPE)+',' +dbo.[fun_get_param_length](@ProName,[name])
		ELSE
        dbo.[fun_get_cssqlpdt_by_sqldt](@DATA_TYPE)
		END)+'),'+CHAR(10),
	@ParamValue=@ParamValue+dbo.fun_get_tabspace(3)+
    (CASE 
    when  [name]='@opr_typ' then 'paras['+cast(@ParamCount as varchar(20))+'].Value = '+@tempProperty+';'
	WHEN @DATA_TYPE='NVARCHAR' OR @DATA_TYPE='VARCHAR' OR @DATA_TYPE='CHAR' OR @DATA_TYPE='NCHAR' OR @DATA_TYPE='NTEXT' OR @DATA_TYPE='TEXT' OR @DATA_TYPE='OUT' or @DATA_TYPE='uniqueidentifier' or @DATA_TYPE='image' or @DATA_TYPE='variant'
    THEN 'paras['+cast(@ParamCount as varchar(20))+'].Value = model.'+@tempProperty+';'
    ELSE
		'if (model.'+@tempProperty+'.Equals('+dbo.[fun_get_cssdt_by_sqldt](@DATA_TYPE)+'.MinValue))'+
		CHAR(10)+dbo.fun_get_tabspace(3)+'{'+
		CHAR(10)+dbo.fun_get_tabspace(4)+'paras['+cast(@ParamCount as varchar(20))+'].Value = null;'+
		CHAR(10)+dbo.fun_get_tabspace(3)+'}'+
		CHAR(10)+dbo.fun_get_tabspace(3)+'else'+
		CHAR(10)+dbo.fun_get_tabspace(3)+'{'+
		CHAR(10)+dbo.fun_get_tabspace(4)+'paras['+cast(@ParamCount as varchar(20))+'].Value = model.'+@tempProperty+';'+
		CHAR(10)+dbo.fun_get_tabspace(3)+'}'
    END)+CHAR(10),
	@ParamCount=@ParamCount+1
	from sys.all_parameters where object_id = object_id(@ProName)
	set @SqlParams=LEFT(@SqlParams,LEN(@SqlParams)-2)
	set @ParamValue=LEFT(@ParamValue,LEN(@ParamValue)-1)

 end
else 
	begin
		print '没有此存储过程!'
		return 0
	end

print dbo.fun_get_tabspace(2)+'#region 执行存储过程'+@ProName+'的函数'
print dbo.fun_get_tabspace(2)+'/// <summary>'
print dbo.fun_get_tabspace(2)+'/// 执行存储过程'+@ProName
print dbo.fun_get_tabspace(2)+'/// <summary>'
if @opr_typstr <>'' 
begin
	print dbo.fun_get_tabspace(2)+'/// <param name="Opr_typ"> 操作类型,1:新增 2:修改,3 删除 </param>'
end
print dbo.fun_get_tabspace(2)+'/// <param name="model">'+@TableName+'对应的model对象 </param>'
print dbo.fun_get_tabspace(2)+'public object Run'+@ProName+'('+@opr_typstr+@TableName+'Model model)'  --    
print dbo.fun_get_tabspace(2)+'{'
print dbo.fun_get_tabspace(3)+'object result=new object();'
print dbo.fun_get_tabspace(3)+'SqlParameter[] paras = new SqlParameter[]'
print dbo.fun_get_tabspace(3)+'{'
print @SqlParams
print dbo.fun_get_tabspace(3)+'};'
print @ParamValue
print dbo.fun_get_tabspace(3)+'try'
print dbo.fun_get_tabspace(3)+'{'
print dbo.fun_get_tabspace(4)+'result = new DbHelper().ExecProDataTable("'+@ProName+'", paras);'
print dbo.fun_get_tabspace(4)+''
print dbo.fun_get_tabspace(3)+'}'
print dbo.fun_get_tabspace(3)+'catch (SqlException ex)'
print dbo.fun_get_tabspace(3)+'{'
print dbo.fun_get_tabspace(4)+'result = null;'
print dbo.fun_get_tabspace(4)+'throw new Exception("数据库操作异常", ex);'
print dbo.fun_get_tabspace(3)+'}'
print dbo.fun_get_tabspace(3)+'return result;'
print dbo.fun_get_tabspace(2)+'}'
print dbo.fun_get_tabspace(2)+'#endregion'

end
/*

exec [pro_GenerateServiceFunction] 'pro_get_Stuinfo','stuinfo'


*/

相关文章
|
7天前
|
存储 SQL 数据库
SQL Server存储过程的优缺点
【10月更文挑战第18天】SQL Server 存储过程具有提高性能、增强安全性、代码复用和易于维护等优点。它可以减少编译时间和网络传输开销,通过权限控制和参数验证提升安全性,支持代码共享和复用,并且便于维护和版本管理。然而,存储过程也存在可移植性差、开发和调试复杂、版本管理问题、性能调优困难和依赖数据库服务器等缺点。使用时需根据具体需求权衡利弊。
|
11天前
|
缓存 C# Windows
C#程序如何编译成Native代码
【10月更文挑战第15天】在C#中,可以通过.NET Native和第三方工具(如Ngen.exe)将程序编译成Native代码,以提升性能和启动速度。.NET Native适用于UWP应用,而Ngen.exe则通过预编译托管程序集为本地机器代码来加速启动。不过,这些方法也可能增加编译时间和部署复杂度。
|
3天前
|
存储 SQL 缓存
SQL Server存储过程的优缺点
【10月更文挑战第22天】存储过程具有代码复用性高、性能优化、增强数据安全性、提高可维护性和减少网络流量等优点,但也存在调试困难、移植性差、增加数据库服务器负载和版本控制复杂等缺点。
|
6天前
|
存储 SQL 数据库
Sql Server 存储过程怎么找 存储过程内容
Sql Server 存储过程怎么找 存储过程内容
10 1
|
8天前
|
存储 SQL 数据库
SQL Server存储过程的优缺点
【10月更文挑战第17天】SQL Server 存储过程是预编译的 SQL 语句集,存于数据库中,可重复调用。它能提高性能、增强安全性和可维护性,但也有可移植性差、开发调试复杂及可能影响数据库性能等缺点。使用时需权衡利弊。
|
14天前
|
存储 SQL 数据库
SQL Server 临时存储过程及示例
SQL Server 临时存储过程及示例
27 3
|
14天前
|
SQL 关系型数据库 MySQL
创建SQL数据库的基本步骤与代码指南
在信息时代,数据管理显得尤为重要,其中数据库系统已成为信息技术架构的关键部分。而当我们谈论数据库系统时,SQL(结构化查询语言)无疑是其中最核心的工具之一。本文将详细介绍如何使用SQL创建数据库,包括编写相应的代码和必要的步骤。由于篇幅限制,本文可能无法达到您要求的2000字长度,但会尽量涵盖创建数
17 3
|
10天前
|
SQL 监控 关系型数据库
SQL错误代码1303解析与处理方法
在SQL编程和数据库管理中,遇到错误代码是常有的事,其中错误代码1303在不同数据库系统中可能代表不同的含义
|
12天前
|
存储 SQL 安全
|
14天前
|
中间件 数据库连接 API
C#数据分表核心代码
C#数据分表核心代码
27 0