C#将list集合转化为sql

将list集合转化为sql in 的范围对象 比如
List<string> idList= new List<string>();
idList.Add(“001”);
idList.Add(“002”);
idList.Add(“003”);

select * from table where id in (‘001’,‘002’,‘001’)

公共方法
/// <summary>
/// C#实现把String字符串转化为SQL语句中的In后接的参数
/// </summary>
/// <param name="aa"></param>
/// <returns></returns>
public static string ToSqlListString(List<string> guidList)
{
StringBuilder sqlString = new StringBuilder();
foreach (string item in guidList)
{
sqlString.AppendFormat("'{0}',", item);
}
string result = sqlString.ToString();
return result.Trim(',');
}

 

posted @ 2023-02-11 21:37  跟着阿笨一起玩.NET  阅读(519)  评论(0编辑  收藏  举报