C# 如何在C语言中有条件地验证SQL数据#

C# 如何在C语言中有条件地验证SQL数据#,c#,mysql,validation,C#,Mysql,Validation,上面的结果和上面的表一样,现在我正在尝试检查“tipo_promonic”列是否包含一些行,因此我正在检查“tipo_promonic”列是否包含Poncentaje、fixed、折扣,然后转到某个函数,否则转到另一个函数ok,但“tipo_promonic”列中包含许多重复的值,因此如何检查条件。请帮我解决这个问题。这是一个数据表。您可以根据需要指定DataAdapter或Dataset。这基本上是一个循环,它检查每一行的特定列的值 I have this code public stat

上面的结果和上面的表一样,现在我正在尝试检查“tipo_promonic”列是否包含一些行,因此我正在检查“tipo_promonic”列是否包含Poncentaje、fixed、折扣,然后转到某个函数,否则转到另一个函数ok,但“tipo_promonic”列中包含许多重复的值,因此如何检查条件。请帮我解决这个问题。

这是一个数据表。您可以根据需要指定
DataAdapter
Dataset
。这基本上是一个循环,它检查每一行的特定列的值

I have this code 
 public static DataSet tipopromocion(string id_edificio,string date_from, string date_to)
    {
        DataSet tipo = new DataSet();

        MySqlConnection con = new MySqlConnection(connectionstring);
        string tipo_promo = "select pr.tipo_promocion from promocion_edificio pe inner join inventario i on i.id_edificio = pe.id_edificio inner join promocion pr on pe.id_promocion=pr.id_promocion where i.id_edificio = '" + id_edificio + "' and i.fecha between '" + date_from + "' and Date_Sub('" + date_to + "',interval 1 Day) and i.fecha between pe.inicio_promo AND pe.fin_promo and date(now()) between pe.inicio_alojamiento and pe.fin_alojamiento AND ( FIND_IN_SET('A',pe.tipo)) group by  pe.id_promocion order by pr.valor_promocion desc";
        MySqlCommand cmd13 = new MySqlCommand(tipo_promo, con);
        MySqlDataAdapter da13 = new MySqlDataAdapter(cmd13);
        da13.Fill(tipo);
        return tipo;                               
    }

tipo_promonic
-------------
porcentaje
porcentaje
fixed
discount
porcentaje
discount
discount
fixed
fixed
porcentaje
porcentaje

您可以根据自己的选择添加更多的
else…如果
。您可以在这些代码块中执行任何操作。

尝试使用DISTINCT关键字重新构建SQL查询的框架,以避免重复记录

foreach (DataRow dr in dataTable.Rows)
{
    if (dr["typo_promonic"].Equals("porcentaje"))
    {
          //your code here.
    }
    else if (dr["tipo_promonic"].Equals("discount"))
    {
          //your code here.
    }
    else
    {
          //your code here.
    }
}
尝试使用DataReader

string tipo_promo = "select DISTINCT pr.tipo_promocion from promocion_edificio pe inner     ..."

我真的很想理解你,但我没有。请澄清并做一些适当的布局。好的,我有上面的一列“tipo_promonic”,它包含一些重复值的行,好的。我想使用条件检查该列中的“Porcentaje,fixed,discount”行,如果检查的条件为真,则it goto diff函数为真,请帮助我了解我的问题?如果这是OP的意图,那么它并没有实现您的期望,因为dr[…]的返回值是
对象
。改为使用'string.Equals(…,…)。我得到的错误是'error 14'System.Data.DataSet'不包含'Rows'的定义,并且找不到接受'System.Data.DataSet'类型的第一个参数的扩展方法'Rows'(是否缺少using指令或程序集引用?)C:\wamp\www\rsrbx\public\u html\API\u Service\Apartplaya API\Apartplaya API\Apartplaya API\Dal.cs 2439 41 Apartplaya API“您将获得。这是如何处理数据表的。试着这样做<代码>字符串qtyInStock=ds.Tables[0]。行[0][index]。toString()然后根据获取的字符串进行检查。无仍然获取错误“无法将类型'char'转换为'System.Data.DataRow'”。也许您应该将数据集分配到数据表中,然后循环该数据表中的所有数据行。并使用
dt.Rows[rowindex][“ColumnName”].ToString()其中dt是您的数据表。从什么时候开始出现重复的问题?我没有读到.k,但在dr[0]中。如果下次dr[0]被更改,则可以使用write column name代替索引,如
dr[“column\u name”]。ToString
我的意思是like if(dr[column\u name]]。ToString()=“option”我可以在单个if中检查多个选项吗?
MySqlConnection con = new MySqlConnection(connectionstring);
string tipo_promo = "select DISTINCT pr.tipo_promocion from promocion_edificio pe inner.."
MySqlCommand cmd13 = new MySqlCommand(tipo_promo, con);
DataReader dr = cmd13.ExecuteReader();
while(dr.Read())
{
  if(dr[0].ToString() == "YourOption")
  {//Do this;}
}