ODP.NET无法从数据读取器读取数据

时间:2013-07-25 16:35:05

标签: asp.net sql oracle odp.net

我是Oracle新手并尝试从Oracle DB获取数据。以下是我的代码。控制台中没有显示错误。但是在代码中不会进入while循环内部。由于我是新手,请帮助我解决这个问题?

public void Login()
        {
            using (OracleConnection connection = new OracleConnection())
            {
                connection.ConnectionString = ConnectionString;

                try
                {
                    LogManager.Info("inside login");
                    connection.Open();
                    OracleCommand cmd = connection.CreateCommand();
                    cmd.Connection = connection;

                    string sql = "select ID from APRV_EMPLOYEE where USERNAME = :username ";
                    LogManager.Info("sql" + sql);
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = sql;
                    cmd.BindByName = true;
                    cmd.Parameters.Add(":username", OracleDbType.NVarchar2).Value = "admin";
                    IDataReader reader = cmd.ExecuteReader();

                    LogManager.Info("Inside DatabaseManager::hasrows");
                    while (reader.Read())
                    {
                        LogManager.Info("Inside DatabaseManager::read" + reader.GetString(1));
                        LogManager.Info("Inside DatabaseManager::read2" + reader.GetString(reader.GetOrdinal("ID")));

                    }

                    reader.Dispose();
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    LogManager.Error("Inside DatabaseManager::Login " + ex.Message);
                }
            }
        }

请帮帮我 提前致谢

1 个答案:

答案 0 :(得分:1)

查看docs,以下是它应该如何运作

string sql = "select ID from APRV_EMPLOYEE where USERNAME = :1 ";
LogManager.Info("sql" + sql);
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
// cmd.BindByName = true;
cmd.Parameters.Add("username", OracleDbType.NVarchar2).Value = "admin";
IDataReader reader = cmd.ExecuteReader();