在c#中创建动态sp并在sql server中执行它 [英] creating dynamic sp in c# and executing it in sql server

查看:156
本文介绍了在c#中创建动态sp并在sql server中执行它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在c#中创建了动态sp语句,因为

Hi,

I have created dynamic sp statement in c# as

 StringBuilder SQLquery = new StringBuilder();
for loop here

SQLquery.Append(" exec  usp_pv_ChangeStatus  4)   
SQLquery.Append(" # ");
SQLquery.Append(" exec  usp_pv_ChangeStatus  4)   
SQLquery.Append(" # ");

  int retinsertTemp = objPvbll.executeSqlquery(SQLquery.ToString(), out suceesFlag);





我有已经在sql server中创建了一个存储过程usp_pv_ChangeStatus



我在sql server中有一个executeql程序,它将执行这个sp





I have already created a stored procedure "usp_pv_ChangeStatus" in sql server

I have a executesql procedure in sql server which will execute this sp

ALTER proc [dbo].[usp_pv_ExecuteSqlquery]
@sqlqueries varchar(max),@suceesFlag int out
as
BEGIN

SET @sqlqueries = REPLACE ( @sqlqueries , '#' , CHAR(13) + CHAR(10) )

PRINT @sqlqueries
exec(@sqlqueries)
end





但它无效,请帮助



but its not working ,please help

推荐答案

请先阅读我对该问题的评论。



你试图实现的方式是错误的!



而是创建动态查询在其中调用sp,直接调用存储过程!

看看这里:如何:创建并执行返回单个值的SQL语句 [ ^ ]

当你在林上打开时k,在窗口的左侧,你会发现很多关于执行查询的有用链接。



我必须警告你关于SQL注入:如何:防止ASP.NET中的SQL注入 [ ^ ]



如果你想使用sp显示数据,请参阅:演练:使用存储过程显示数据在GridView Web服务器控件中 [ ^ ]
Please, read my comment to the question first.

The way you're trying to achieve that is wrong!

Instead creating "dynamic query" to call sp in it, call stored procedure directly!
Have a look here: How to: Create and Execute an SQL Statement that Returns a Single Value[^]
When you open above link, on the left side of window, you'll find many usefull links about executing queries.

I must warn you about SQL Injection: How To: Protect From SQL Injection in ASP.NET[^]

If you would like to display data using sp, please see: Walkthrough: Displaying Data Using a Stored Procedure in the GridView Web Server Control[^]


这篇关于在c#中创建动态sp并在sql server中执行它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆