0

我尝试从数据库中接收用户的昵称,但它始终返回分配给字符串昵称变量的值。

    public string GetEigenaarBlog(int gebruikerid)
    {
        string nickname = null;
        try
        {
            connection.Open();
            string sql = "SELECT Nickname FROM Gebruiker WHERE GebruikerID = :gebruikerid";
            command = new OracleCommand(sql, connection);
            command.Parameters.Add(new OracleParameter("gebruikerid", gebruikerid));
            nickname = Convert.ToString(command.ExecuteReader());
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            connection.Close();
        }
        return nickname;
    }

这是我的代码形式:

    private void listBoxBerichten_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListBox lb = (ListBox)sender;
        Blog blog = (Blog)lb.Items[lb.SelectedIndex];
        int blogid = blog.BlogID;

        geselecteerdeBlog = dk.GetGeselecteerdeBlog(blogid);

        string blogeigenaar;
        foreach (Blog b in geselecteerdeBlog)
        {
            blogeigenaar = dk.GetEigenaarBlog(b.GebruikerID); //This is the method where is the problem
            tbGeblogd.Text = Convert.ToString(b.Datum);
            tbTitel.Text = b.Titel;
            tbDoor.Text = blogeigenaar;
            tbBlogInhoud.Text = b.Inhoud;
        }
    }

该参数有效,它从表单中读取该参数。

当我更改string nickname = nullstring nickname = 'hello'then 它返回昵称作为你好。所以它返回分配的值。当我保留时string nickname = null,它返回 null

我究竟做错了什么?SQL查询是正确的,并且用户存在于数据库中。我没有收到任何错误或警告。

谢谢!

4

2 回答 2

4

您的代码需要以这种方式更改

public string GetEigenaarBlog(int gebruikerid)
{
    string nickname = null;
    try
    {
        connection.Open();
        string sql = "SELECT Nickname FROM Gebruiker WHERE GebruikerID = :gebruikerid";
        command = new OracleCommand(sql, connection);
        command.Parameters.Add(new OracleParameter("gebruikerid", gebruikerid));
        OracleDataReader reader = command.ExecuteReader();
        // Now try to read from the reader (and position the reader on the first record returned)
        if(reader.Read())
            nickname = reader[0].ToString();
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        connection.Close();
    }
    return nickname;

ExecuteReader 方法返回一个 OracleDataReader,并且在尝试读取该对象之前,该对象需要位于第一条记录上。因此,您需要调用 Read 方法,如果该方法返回 true,您就可以读取字符串。

但是,如果您有一个只返回一行和一列的查询,那么一种非常快速的方法是通过 ExecuteScalar 方法

        connection.Open();
        string sql = "SELECT Nickname FROM Gebruiker WHERE GebruikerID = :gebruikerid";
        command = new OracleCommand(sql, connection);
        command.Parameters.Add(new OracleParameter("gebruikerid", gebruikerid));

        // ExecuteScalar returns the value of the first row/first column, or null if 
        // there is no record to return. Need to be carefull here
        object result = command.ExecuteScalar();
        if(result != null)
            nickname = result.ToString();

作为旁注,我看不到完整的代码,但似乎您保留了一个全局连接对象。
这被认为是一种不好且不必要的做法,因为连接池机制可以更好地保持连接对象随时可用

于 2013-06-16T11:54:32.997 回答
1

您不能以这种方式访问​​数据读取器。您将不得不遍历数据读取器并获取数据。有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/system.data.oracleclient.oracledatareader.aspx

代替

nickname = Convert.ToString(command.ExecuteReader());

尝试这个

OracleDataReader reader = command.ExecuteReader();
nickname = "";
while (reader.Read())
{
    nickname = Convert.ToString(reader[0]));
}
于 2013-06-16T11:54:41.370 回答