Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将参数化sql查询作为参数传递给不工作的存储过程_C#_Sql_Sql Server_Tsql_Stored Procedures - Fatal编程技术网

C# 将参数化sql查询作为参数传递给不工作的存储过程

C# 将参数化sql查询作为参数传递给不工作的存储过程,c#,sql,sql-server,tsql,stored-procedures,C#,Sql,Sql Server,Tsql,Stored Procedures,我想将参数化SQL查询作为参数传递给SQL Server中的存储过程,但无法使其工作。这就是我尝试过的 存储过程代码: CREATE PROCEDURE [dbo].[SroredProc] @Qry nvarchar(max) AS BEGIN SET NOCOUNT ON; EXEC sp_executesql @Qry; End C代码 SqlConnection con = new SqlConnection(ConfigurationManager.Connecti

我想将参数化SQL查询作为参数传递给SQL Server中的存储过程,但无法使其工作。这就是我尝试过的

存储过程代码:

CREATE PROCEDURE [dbo].[SroredProc]
   @Qry nvarchar(max)
AS
BEGIN
   SET NOCOUNT ON;
   EXEC sp_executesql @Qry;
End
C代码

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString);
SqlCommand cmd = new SqlCommand();
string s = "select id,name from tbl where id=@id";
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = s;
cmd.Parameters.AddWithValue("@id", 1);
cmd= new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SroredProc";
cmd.Parameters.AddWithValue("@Qry", s);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);

我想你应该像下面这样。再说一次,这么早打开连接是没有意义的;而是在调用/执行命令之前打开它

int id = 1;
SqlConnection con = new 
SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString);
SqlCommand cmd = new SqlCommand();
string s = string.format("select id,name from tbl where id={0}",id);
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SroredProc";
cmd.Parameters.AddWithValue("@Qry", s);
DataTable dt = new DataTable();
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
有两件事:

您忘记将连接与命令关联

cmd.Connection = con;
我会将字符串s的声明改为,@Qry参数永远不会用id的实际值填充

int id = 1; // <- insert your value here.
string s = String.Format("select id,name from tbl where id={0}", id);
//cmd.Parameters.AddWithValue("id", 1);  <-- remove this line

请看一下我添加到代码中的注释


我真的明白你想做什么。您正在尝试参数化发送到存储过程的tsql语句,然后参数化存储过程。不幸的是,你不能这样做。您可以参数化TSQL语句。不能参数化参数

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString);
SqlCommand cmd = new SqlCommand();
string s = "select id,name from tbl where id=@id";
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = s;
cmd.Parameters.AddWithValue("@id", 1);  // You set the param here
cmd= new SqlCommand();  // You just effectively erased the previous 4 lines of code with this line.
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SroredProc";
cmd.Parameters.AddWithValue("@Qry", s);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);

你确定?仔细检查柱子。。它已经存在了。忽略了这一点,你是对的。是的,错误在于他在定义所有属性之前过早地打开了连接方式。在提供这里所需的每个值时,我得到的StoredProc有太多的参数specified@user3472352,不确定我是否理解,但根据您的PostStoredProc,它只接受您传入代码的一个参数。在你的帖子中还有什么你没有提到的吗?没有,我已经写了所有需要的东西我希望它不受sql注入的影响,这就是为什么需要将它参数化,以便您的解决方案字符串s=string.formatselect id,name from tbl,其中id={0},id;听起来不太好我真的明白你想做什么。您正在尝试参数化发送到存储过程的tsql语句,然后参数化存储过程。不幸的是,你不能这样做。您可以参数化TSQL语句。您不能参数化参数。在这种情况下,还有什么可以阻止sql注入的吗?如果有任何帮助,我将不胜感激。在我看来,我会改变设计。这意味着什么?请你详细说明一下。你的目标是不依赖任何时候吗?我只想在查询中添加No Count,而不使用存储过程。我有一种感觉,您使用此存储过程的唯一原因是您在某个地方读到存储过程有助于SQL注入。这是正确的,但它不适用于此用途。去掉存储过程并参数化您在“S”中的TSQL语句。这似乎是一个非常糟糕的主意。将参数化查询传递给存储过程有什么好处?如果已经正确地参数化了,就执行它吧!