遍历数据表以获取每一行的具体值

时间:2020-08-29 21:09:13

标签: c# datatable

当遍历此数据表时,无法在if else语句中使用存储的值时,出现以下错误 系统无法执行指定的程序。 我期望发生的是if else块来检查toValue并从那里去 ps-先前问题looping thru datatable values的延续 edit *为清楚起见添加了完整的代码块

 string conString = "user id =user1; password =password;" + "data source = database.home.org";

        OracleConnection con = new OracleConnection
        {
            ConnectionString = conString
        };

        con.Open();
       var pullRecord = new OracleCommand("select * FROM email_records", con);
        var dt= new DataTable();
        var da = new OracleDataAdapter(pullRecord);
             da.Fill(dt);

        for (int x = 0; x < dt.Rows.Count; x++)
        {

            string toValue = (dt.Rows[x].ItemArray[0].ToString() + " ");
            string ccValue = (dt.Rows[x].ItemArray[1].ToString() + " ");
            string bodyValue = (dt.Rows[x].ItemArray[4].ToString() + " ");

            if(toValue == "one" || toValue == "two" || toValue == "three")
            {
                Console.WriteLine(bodyValue);
            }
            else if (toValue == "four" || toValue == "five")
            {
                Console.WriteLine(bodyValue);
            }
            else if (toValue == "six" || toValue == "seven")
            {
                Console.WriteLine(bodyValue);
            }
            else if (toValue == "eight" || toValue == "nine")
            {
                Console.WriteLine(bodyValue);
            }
            else if (toValue == "ten" || toValue == "eleven")
            {
                Console.WriteLine(bodyValue);
            }
            else if (toValue == "twelve")
            {
                Console.WriteLine(bodyValue);
            }
            else if (toValue == "thirteen")
            {
                Console.WriteLine(bodyValue);
            }
            else
            {
                Console.WriteLine(bodyValue);
            }
        }

0 个答案:

没有答案