[英]How to fetch records from table having column with comma separated values using c# asp.net?
i was trying to get records from comma separated values from MySQL database by passing checkboxlist selected values in where clause using c# asp.net.我试图通过使用 c# asp.net 在 where 子句中传递复选框列表选择的值来从 MySQL 数据库中获取逗号分隔值的记录。
example:- i have table in following manner示例:- 我有以下方式的表
1 | 1 | ImgPath |
图片路径 | Red,Green
红色,绿色
2 | 2 | ImgPath |
图片路径 | Orange,Yellow
橙色、黄色
<asp:CheckBoxList runat="server" ID="Chk_Colors" Font-Size="Small"
AutoPostBack="true" CssClass="chk1"
OnSelectedIndexChanged="Chk_Colors_SelectedIndexChanged" RepeatColumns="3"
RepeatDirection="Horizontal">
<asp:ListItem> Red </asp:ListItem>
<asp:ListItem> Green </asp:ListItem>
<asp:ListItem> Orange </asp:ListItem>
</asp:CheckBoxList>
<asp:Repeater ID="rptr_data" runat="server">
<ItemTemplate>
<div class="display-image-box" >
<img class="display-img" src='<%# Eval("tshirt_img") %>'>
</div>
<div class="display-image-box" >
<h1><%# Eval("Color") %> </h1>
</div>
</ItemTemplate>
<asp:Repeater>
protected void Chk_Colors_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string selecteditems = String.Join(",",Chk_Colors.Items.OfType<ListItem>().Where(r
=> r.Selected).Select(r => r.Text));
string valueschk = selecteditems;
string str = "select * from tbl_colors where Color IN ('" +selecteditems+ "')";
MySqlConnection con1 = new MySqlConnection(constr);
MySqlCommand cmd1 = new MySqlCommand(str, con1);
MySqlDataAdapter da1 = new MySqlDataAdapter();
da1.SelectCommand = cmd1;
DataTable dt1 = new DataTable();
da1.Fill(dt1);
rptr_data.DataSource = dt1;
rptr_data.DataBind();
}
catch (Exception ex)
{
}
}
The only correct answer to your problem is to fix the real problem of a bad database design.您的问题的唯一正确答案是解决糟糕的数据库设计的真正问题。 From experience we know that every hack you have to implement to cope with bad design will make the amount of code needed to solve a trivial problem grow exponentially.
根据经验我们知道,为应对不良设计而必须实施的每一次 hack 都会使解决一个小问题所需的代码量呈指数级增长。 And one day it will be impossible to maintain and extend your code using a healthy or profitable balance of time an resources.
有一天,将不可能使用健康或有利可图的时间和资源平衡来维护和扩展您的代码。 Complexity will introduce more bugs than you can fix (as those fixes will likely introduce other bugs).
复杂性将引入比您可以修复的更多的错误(因为这些修复可能会引入其他错误)。
Your problem is that you are violating the data atomicity rule: each column must only contain a single value.您的问题是您违反了数据原子性规则:每列只能包含一个值。 Following this rule will always lead to simple code to handle queries and allows you to use strong column types (for example numeric instead of text columns).
遵循此规则将始终生成简单的代码来处理查询,并允许您使用强列类型(例如数字而不是文本列)。 You would never have asked your question if you had followed this rule.
如果你遵循这条规则,你永远不会问你的问题。
This kind of design optimization is very common and called "database normalization".这种设计优化很常见,称为“数据库规范化”。
There are usually three normal forms that you should always try to implement.通常有三个正常的 forms,您应该始终尝试实现。 Actually, your problem is the very first normalization step or normal form, referred to as "1NF" (first normal form).
实际上,您的问题是第一个规范化步骤或范式,称为“1NF” (第一范式)。
You should read What is Database Normalization?您应该阅读什么是数据库规范化? .
. It's a very brief article with a nice example.
这是一篇非常简短的文章,有一个很好的例子。 The example applies the normal forms step by step.
该示例逐步应用正常的 forms。 I'm sure you can apply the example directly to your database without any effort (it's less effort than botching your database handling, for sure).
我相信您可以毫不费力地将示例直接应用于您的数据库(这肯定比搞砸您的数据库处理要省力)。
I guess that this is not the answer you want to hear, but this is the only answer any serious developer can give.我猜这不是你想听到的答案,但这是任何认真的开发人员能给出的唯一答案。 Your current problem, and those many more to come following up, is solved by some very simple design changes.
您当前的问题,以及接下来要解决的更多问题,都可以通过一些非常简单的设计更改来解决。 It's a problem that shouldn't be there in the first place ie it's not a normal problem.
这是一个本来就不应该存在的问题,即它不是一个正常问题。 Don't already mess up the foundation.
不要已经搞砸了基础。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.