偶数和奇数值 [英] even and odd value counts

查看:68
本文介绍了偶数和奇数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有当Image_ID1和Image_ID2都不为空时,这些方法才会给出结果。

如果其中一个或两个都为空,则显示错误。

此外,它只返回偶数值。奇数值计数被跳过。

示例,列Image_ID1 = 1,3,null和Image_ID2 = 2,4,6。

它必须显示5个值,但它显示这个空值的4个值会产生原因。
如何解决这些问题?



These methods give result, only if both Image_ID1 and Image_ID2 are not null.
If one of them or both of them are null, then it shows error.
Besides it returns only even value counts. Odd value counts are skipped.
Example, Column Image_ID1=1,3,null and Image_ID2=2,4,6.
It has to show 5 values but it shows 4 values cause of that null value counts.
How can I solve these issues?

private void BindGrid()
    {
        MySqlConnection con = new MySqlConnection(constr);
        MySqlCommand cmd = new MySqlCommand("SELECT * FROM images where Image_ID in (" + String.Join(",", getImage_ID()) + ")", con);
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        gvImages.DataSource = dt;
        gvImages.DataBind();
    }
    private List<int> getImage_ID()
    {
        List<int> i = new List<int>();
        MySqlConnection con = new MySqlConnection(constr);
        con.Open();
        string query = "Select Image_ID1, Image_ID2 from register where students_ID='" + getStudents_ID() + "'AND Image_ID1 IS NOT NULL AND Image_ID2 IS NOT NULL"; 
        MySqlCommand cmd = new MySqlCommand(query);
        cmd.Connection = con;
        MySqlDataReader reader = cmd.ExecuteReader();
        foreach (DbDataRecord s in reader)
            {
                i.Add(s.GetInt32(0));
                i.Add(s.GetInt32(1));
            }
        return i;
    }

推荐答案

string query = "Select Image_ID1, Image_ID2 from register where students_ID='" + getStudents_ID() + "'"; 



并在


and check null in

foreach (DbDataRecord s in reader)
            {
                if (s["Image_ID1"] != DBNull.Value){
                   i.Add(s.GetInt32(0));
                }
                if (s["Image_ID2"] != DBNull.Value){
                  i.Add(s.GetInt32(1));
                }
            }


这篇关于偶数和奇数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆