简体   繁体   English

如何使用 c# asp.net 从具有逗号分隔值的列的表中获取记录?

[英]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示例:- 我有以下方式的表

ID |编号 | tshirt_img | tshirt_img | Color颜色

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.

相关问题 如何在C#linq asp.net MVC中使用逗号分隔值列连接两个表 - how to join two tables with comma separated values column in c# linq asp.net mvc 从表中筛选具有逗号和管道分隔值的列 - Filter from table for column having comma and pipe separated values 如何在Asp.net MVC C#中使用Linq从一个表中选择具有最大计数值的多个表中的记录# - how to select records from multiple table with max count value from one table using Linq in Asp.net MVC C# 如何在C#的datagridview列中添加逗号分隔的值 - how to add values with separated comma in datagridview column in c# 如何在C#ASP.NET MVC项目中使用ajax更新SQL表中的记录顺序? - How to update orders of records in SQL table using ajax in C# ASP.NET MVC project? 如何使用asp.net core 2.2从bson数组中形成逗号分隔的字符串? - how to form comma separated string from bson array using asp.net core 2.2? 如何在c#中的变量中从sql sever传递逗号分隔的列值 - How to pass comma separated column values from sql sever in a variable in c# 在Asp.net中填充逗号分隔的字符串以多选ListBox-C# - Populate comma separated string to multi select ListBox in Asp.net - C# 如何将多个值从listview列存储到数据库asp.net的单个条目C# - how to store multiple values from listview column to a single entry to database asp.net c# 如何使用asp.net和c#从不同的表创建带有自定义列的网格视图? - how to create Grid view with customized column from different table with asp.net and c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM