场景如下:select id from table1 where col1 = "value";
select id from table1 where col2 = "value2";
select id from table2 where col1 = "value3";
select id from table3 where col1 = "value4";
......就是说,表名不定,字段名不定,如何将这种sql语句参数化传递?
不使用第三方的持久化框架,如何做到一个sql语句类似这种:
select ? from ? where ? = "xxxxx";望高手指点!
select id from table1 where col2 = "value2";
select id from table2 where col1 = "value3";
select id from table3 where col1 = "value4";
......就是说,表名不定,字段名不定,如何将这种sql语句参数化传递?
不使用第三方的持久化框架,如何做到一个sql语句类似这种:
select ? from ? where ? = "xxxxx";望高手指点!
CREATE PROCEDURE ProceName
@Parameter varchar(500),
@Condition varchar(500)
AS
DECLARE @Sql varchar(8000)SET @Sql='
SELECT
[Col0] ,
[Col1] ,
FROM
[dbo].[Table]
WHERE 1=1'IF(@Condition<>'')
BEGIN
SET @Sql=@Sql+' AND '+@Condition
ENDSET @Sql=@Sql
EXEC(@Sql)
GO
兼容:
1.传进来的参数可以是单表,多表
2.可以返回数据集,可以返回单个值谢谢。
{
string connString = "server=.;uid=sa;pwd=sa;database=datas";
using (SqlConnection conn = new SqlConnection(connString))
{
string sql = string.Format("select {0} from {1} where {2} = @para1", selectList, tableName, colName);
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter para1 = new SqlParameter("@para1", SqlDbType.NVarChar);
para1.Value = colValue;
cmd.Parameters.Add(para1);
// ...
}
}
以后统一调用