从oracle数据库中提取数据

时间:2016-05-03 15:00:45

标签: c# oracle visual-studio

我阅读了很多关于从visual studio连接到oracle数据库的内容,我在这里:

public string ExecuteSelect(string query)
{
    string result = "";
    int i = 0;
    string oradb = "Data Source=FG50OP02;User Id=****;Password=****;";
    using (OracleConnection conn = new OracleConnection(oradb))
    {
        conn.Open();
        using (OracleCommand cmd = new OracleCommand(query, conn))
        {
            OracleDataReader reader = cmd.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    result = result + (string)reader.GetOracleString(i);
                    i++;
                }
            }
            else
            {
                MessageBox.Show("No rows found.");
            }
            reader.Close();
        }
        conn.Dispose();
    }

    return result;
}

我收到错误

  

“ORA-000911无效的人物”

但我的查询非常简单,“select AAA from BBB”。它返回(当我直接在toad上执行它时)只是一个标志“Y”或“N”。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

谢谢大家,所以这对我有用:

public string[] ExecuteSelect(string query)
    {
        string[] result = { "", "", ""};
        string oradb = "Data Source=FG50OP02;User Id=AUTOEIM;Password=EIM;";
        using (OracleConnection conn = new OracleConnection(oradb))
        {
            conn.Open();
            using (OracleCommand cmd = new OracleCommand(query, conn))
            {
                OracleDataReader reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        result[0] = (string)reader["ROW_ID"];
                        result[1] = (string)reader["NAME"];
                        result[2] = (string)reader["LOGIN"];
                    }
                }
                else
                {
                    MessageBox.Show("No rows found.");
                }
                reader.Close();
            }
            conn.Dispose();
        }

        return result;
    }

它准确地返回了TOAD上的内容。