本文介绍了显示到CheckBoxList使用c#在asp.net中的单列数据库中使用逗号分隔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个文本框,DropDownList,复选框列表。 银金,金,纯银,银黄铜 4字符串插入单列但以逗号分隔的值..如(银色金色,银色黄铜色)或(金色,纯银色,银色黄铜色) 然后访问Checkboxlist相关的值可以复选框可以查看.... ??? 问题: - 数据保存在数据库中但是不妥善保存..我的意思是逗号值s .... 我的代码是: - protected void btnADDSAVE_Click(object sender,EventArgs e) { InsertMetal(); } protected void InsertMetal() { SqlConnection con = new SqlConnection(str); SqlCommand cmd = new SqlCommand(); cmd.CommandText =sp_addnewproduct; cmd.CommandType = CommandType.Stor edProcedure; cmd.Parameters.Add(@ name,SqlDbType.VarChar)。 Value = txtName.Text.Trim(); cmd.Parameters.AddWithValue(@ category,DropDownList1.SelectedItem.Value); // SqlParameter metal = new SqlParameter(@ metal,SqlDbType.VarChar); ; // metal.Value = CheckBoxList1.SelectedValue; // cmd.Parameters.Add(metal); cmd.Parameters.AddWithValue(@ metal,CheckBoxList1.SelectedItem.Text.Trim()); cmd.Connection = con; string baby =; for(int i = 0; i< CheckBoxList1.Items.Count; i ++) { if(CheckBoxList1.Items [i] .Selected) { baby + = CheckBoxList1.Items [i] .Value +,; } } 宝贝= baby.TrimEnd(','); Response.Write(宝贝); 试试 { con.Open(); cmd.ExecuteNonQuery(); Response.Write(已添加); } catch(例外e) // catch { throw e; //回应。写(不添加); //返回宝贝; } 终于 { con.Close(); con.Dispose(); } } i have one textbox,DropDownList,checkboxlist.Silver Gold, Gold,Sterling Silver,Silver Brass4 String insert in Single Column but comma separated values.. like(Silver Gold,Silver Brass) or(Gold,Sterling Silver,Silver Brass)then Access the Checkboxlist related the value can checkbox can checked....???Problem:- data save in database but not properly save.. i mean comma saparated values....My Code Is:- protected void btnADDSAVE_Click(object sender, EventArgs e) { InsertMetal(); } protected void InsertMetal() { SqlConnection con = new SqlConnection(str); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "sp_addnewproduct"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = txtName.Text.Trim(); cmd.Parameters.AddWithValue("@category", DropDownList1.SelectedItem.Value); //SqlParameter metal = new SqlParameter("@metal", SqlDbType.VarChar); ; // metal.Value = CheckBoxList1.SelectedValue; // cmd.Parameters.Add(metal); cmd.Parameters.AddWithValue("@metal", CheckBoxList1.SelectedItem.Text.Trim()); cmd.Connection = con; string baby = ""; for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) { baby += CheckBoxList1.Items[i].Value + ","; } } baby = baby.TrimEnd(','); Response.Write(baby); try { con.Open(); cmd.ExecuteNonQuery(); Response.Write("Added"); } catch (Exception e) //catch { throw e; //Response.Write("NOT Added"); // return baby; } finally { con.Close(); con.Dispose(); } }推荐答案 "Silver Gold, Gold,Sterling Silver,Silver Brass" ,这是一个PITA! 因此,如果您的数据库将这些存储在具有外键链接的单独表中,那么这是正确的方式!您不显示表格设计,但在SQL中将条目列表转换为CSV非常简单: in SQL, that's a PITA!So if your DB has these stored in separate tables with foreign key links, than that's the right way! You don't show your table design, but converting a list of entries to a CSV in SQL is pretty easy:SELECT SUBSTRING((SELECT ',' + MyValue FROM myTableFOR XML PATH('')),2,100000) AS CSV 这篇关于显示到CheckBoxList使用c#在asp.net中的单列数据库中使用逗号分隔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 22:55