select * form table where Id in (数组的问题)

yjjone 2014-12-15 12:21:35
如题语句,三层架构,需要在 (@Ids)内传入一个数组 ,
实现的功能就是 判断哪些行被选中,然后把选中行的ID加入到数组中,然后用sql查询出对应的数据
UI 层
private void Print_Click(object sender, RoutedEventArgs e)
{
List<int> Ids= new List<int>();

List<T_CpckLibraryNO> xelementList = grd.SelectedItems.OfType<T_CpckLibraryNO>().ToList();

foreach (T_CpckLibraryNO tq in xelementList)
{
LibraryNos.Add(tq.Id);
}
string str="";
for (int i = 0; i < Ids.Count; i++)
{

str = str + Ids[i] + ","; //每个中间加逗号
}

str = str.Substring(0, str.Length - 1); //去掉最后一个逗号




DAL 层
public DataTable getbyid(string Ids)
{
DataTable table = SqlHelper.ExecuteDataTable("select * from T_CpckLibraryNO where Ids in (@Ids) ", new SqlParameter("@Ids", Ids));


if (table.Rows.Count <= 0)
{
return null;
}
else if (table.Rows.Count == 1)
{
return table;
}
else
{
throw new Exception();
}
}


变量str 传进去 被识别为 “” 多了两个引号,所以一直查不到数据了 难道要把 str变量 转换成其他类型?
请问该如何取消引号 搜索了下论坛都没试验成功
...全文
583 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
大聪 2015-01-23
  • 打赏
  • 举报
回复
那就不用参数了直接 "select * from T_CpckLibraryNO where Ids in (“ + ids +”) ";
宇峰科技 2015-01-23
  • 打赏
  • 举报
回复
各种奇葩问题,醉了,累了。碎觉
wilsonhong 2015-01-08
  • 打赏
  • 举报
回复
比如说你选中的是1 3 5 字符串格式是1,3,5,你先手动吧她变成 ,1,3,5, 你数据库要查询出符合这个条件的比如 select * from table1 where ',1,3,5,' like '%,'+table1.id+',%'
  • 打赏
  • 举报
回复
使用cast 转换一下。
灬浪子灬 2014-12-15
  • 打赏
  • 举报
回复
引用 楼主 yjjone 的回复:
变量str 传进去 被识别为 “” 多了两个引号,所以一直查不到数据了 难道要把 str变量 转换成其他类型? 请问该如何取消引号 搜索了下论坛都没试验成功
for (int i = 0; i < Ids.Count; i++) { str+="'" + Ids[i] "'" + ","; } ConnStrSql = ConnStrSql.Substring(0, ConnStrSql.Length - 1); sqlWhere= 拼接 string.Format(IN ({0}),ConnStrSql )

8,756

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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