在c# asp . net中使用List方法
本文关键字:List 方法 asp net | 更新日期: 2023-09-27 18:06:15
我已经为单个用户添加了一个收藏颜色列表方法,存储在doTableUsers中:
List<string> colorList = new List<string>();
....
if (reader.HasRows)
{
while (reader.Read())
{
idColor = reader["idColor"].ToString();
colorList.Add(idColor.ToString());
}
ns = "";
foreach (string s in colorList)
{
if (ns.Length != 0)
{
ns += ", ";
}
ns += s;
}
}
现在我需要在GridView中为单个用户显示他们喜欢的颜色。
如果在Gridview方法中编写代码:
Response.Write(str.ToString());
在输出中,我有正确的颜色列表:
Antique White, Blueviolet, Dark Blue
但是在query中传递的参数show the GridView是不正确的,逗号丢失了:
Antique WhiteBluevioletDark Blue
下面是我的代码,提前感谢。
str = null;
strArr = null;
count = 0;
str = ns == null ? "" : ns.ToString();
char[] splitchar = { ',' };
if (!string.IsNullOrEmpty(str))
{
strArr = str.Split(splitchar);
}
else
{
strArr = null;
}
for (count = 0; count <= strArr.Length - 1; count++)
{
sql = "......";
}
DataSet dsProducts = new DataSet();
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand(sql, cn))
{
for (count = 0; count <= strArr.Length - 1; count++)
{
cmd.Parameters.AddWithValue("param1", Server.UrlDecode(strArr[count].Trim()));
Response.Write(Server.UrlDecode(strArr[count].Trim()));
}
}
}
不是你问题的答案,但是
ns = "";
foreach (string s in colorList)
{
if (ns.Length != 0)
{
ns += ", ";
}
ns += s;
}
可以转换成
var ns = string.Join(", ", colorList.ToArray());
就做var newValues = str.Split(',');