我尝试从数据库中接收用户的昵称,但它始终返回分配给字符串昵称变量的值。
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 = null
为string nickname = 'hello'
then 它返回昵称作为你好。所以它返回分配的值。当我保留时string nickname = null
,它返回 null
我究竟做错了什么?SQL查询是正确的,并且用户存在于数据库中。我没有收到任何错误或警告。
谢谢!