SQL in() 和base.Dispose(disposing)的问题求助。。。

jacksonhf 2015-03-11 11:18:31
 
public DataSet get_doc(string cuid,int doctype)
{
SqlParameter[] pars ={
MakeInParam("@cuid",SqlDbType.NVarChar,200,cuid),
MakeInParam("@doctype",SqlDbType.TinyInt,4,doctype),
};
return RunProc("select a.*, b.flow_name, c.node_name, c.node_type_id, c.node_order_id, c.node_next_id from t_doc a, t_flow b, t_node c where a.df = 0 and a.cu in (@cuid) and a.st in ( 0,1,4 ) and a.doc_type = @doctype and a.flow_id *= b.flow_id and a.node_id *= c.node_id order by a.doc_id desc", pars, "t_doc123");
}


public DataSet RunProc(string procN, SqlParameter[] prams, string tbN)
{
SqlDataAdapter dap = CreateDataAdaper(procN, prams);
DataSet ds = new DataSet();
dap.Fill(ds, tbN);
this.Close();
return ds;
}


private SqlDataAdapter CreateDataAdaper(string procN, SqlParameter[] prams)
{
this.Open();
SqlDataAdapter dap = new SqlDataAdapter(procN,con);
dap.SelectCommand.CommandType = CommandType.Text;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
dap.SelectCommand.Parameters.Add(parameter);
}
dap.SelectCommand.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));

return dap;
}


问题出在这个语句:
select a.*, b.flow_name, c.node_name, c.node_type_id, c.node_order_id, c.node_next_id from t_doc a, t_flow b, t_node c where a.df = 0 and a.cu in (@cuid) and a.st in ( 0,1,4 ) and a.doc_type = @doctype and a.flow_id *= b.flow_id and a.node_id *= c.node_id order by a.doc_id desc
的in (@cuid)
程序会跳到:

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

如果不传参数,直接in(1,2,3,4)这样。程序就没问题了。
请问有为什么和怎么解决呢?
...全文
316 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
於黾 2015-03-11
  • 打赏
  • 举报
回复
in就是规定了查询条件,字段值必须等于括号里传入的所有字段值的其中一个 跟写一堆or是一个效果 你不能把or作为参数传入
於黾 2015-03-11
  • 打赏
  • 举报
回复
in(,,,)这是个语法,每个用逗号隔开的,都是字段值,而逗号本身是语法的一部分 好比select * from 表 你不能把select作为参数传入
jacksonhf 2015-03-11
  • 打赏
  • 举报
回复
引用 2 楼 Z65443344 的回复:
你需要理解什么是参数化查询,为什么要传参数 不要把它想象成传递了参数之后还是直接在数据库里拼接字符串那么简单 如果真是那样,参数化查询就没有任何存在的意义了,自己程序里直接拼接不一样吗
程序里写可以,只是方法都写好了。 string cuid="1,2,3,4"; 再把cuid作为参数发过去不行吗? in()里面放的是什么类型呢?
於黾 2015-03-11
  • 打赏
  • 举报
回复
每个参数只能是一个字段值,而逗号是语法,你把逗号放进参数里,整个语法都出错了,当然是无法执行成功的 参数里真的能放语法的话,那么参数化查询也根本解决不了SQL注入的问题了 正是因为参数就是参数,里面没有语法,才能解决SQL注入的问题
於黾 2015-03-11
  • 打赏
  • 举报
回复
你需要理解什么是参数化查询,为什么要传参数 不要把它想象成传递了参数之后还是直接在数据库里拼接字符串那么简单 如果真是那样,参数化查询就没有任何存在的意义了,自己程序里直接拼接不一样吗
於黾 2015-03-11
  • 打赏
  • 举报
回复
不要把"1,2,3,4"作为一个参数,这明明应该是4个参数
HelloWorld_HZS 2015-03-11
  • 打赏
  • 举报
回复
试试这个: declare @cuid varchar(200),@doctype int set @cuid='1,2,3,4,5‘ set @doctype=1 select a.*, b.flow_name, c.node_name, c.node_type_id, c.node_order_id, c.node_next_id from t_doc a, t_flow b, t_node c where a.df = 0 and charindex(','+rtrim(a.cu)+',',','+@cuid+',')>0 and a.st in ( 0,1,2,4 ) and a.doc_type = @doctype and a.flow_id *= b.flow_id and a.node_id *= c.node_id order by a.doc_id desc 99%成功,成功了分给我。其他的回答理论一大堆,来的实际的行不?
HelloWorld_HZS 2015-03-11
  • 打赏
  • 举报
回复
十分怀疑你的a.cu在数据库里的类型应该是int,不然就算传了字符串最多就得不到结果,不会出错的。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧