本文介绍了如何动态地将选定的列和表作为存储过程中的参数传递。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我的问题是,我有两个DropDownList。



1.从第一个DropDownList用户将选择表。

2.从第二个DropDownList用户将根据DropDownList 1st中的选定表选择多个列。



所以我想传递选定的表(来自第一个ddl)和多个列(来自第二个ddl)作为sql存储过程中的参数。



注意:我想将列作为数组传递。



请帮助。

Hello,
My question is like ,i have two DropDownList.

1. From first DropDownList user will select table.
2. From second DropDownList user will select multiple columns based on the selected table from DropDownList 1st.

So i want to pass selected table (from 1st ddl) and multiple columns (from 2nd ddl) as a parameters in sql Stored Procedure.

Note: i want to pass columns as array.

Please Help.

推荐答案

string strCols = "";
foreach (string strVal in DropDownList1.SelectedIValues)
{
      strCols = strCols + strVal + ",";
}



将strCols传递给存储过程


pass strCols to stored procedure

cmd.Parameters.AddWithValue("@Cols", strCols);







SQL






SQL

ALTER PROCEDURE [dbo].[proc_test]
(
   @cols nvarchar (MAX),
   @tablename nvarchar(250)
)
AS
DECLARE @Query varchar(MAX)
SET @Query = 'SELECT ' + @cols + ' from ' + @tablename

EXEC(@Query)


这篇关于如何动态地将选定的列和表作为存储过程中的参数传递。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 15:39