C# SqlReader对象不读取

C# SqlReader对象不读取,c#,sql,asp.net,sql-server,C#,Sql,Asp.net,Sql Server,我正在使用SqlReader对象(reader)将Sql server数据库中的字符串值分配给图像的ImageUrl,但是ImageUrl始终为空,而reader对象在调试时不满足while循环条件 protected void Button3_Click(object sender, EventArgs e) { string query = "select profilePicture from Users where username = '@username'";

我正在使用
SqlReader
对象(
reader
)将Sql server数据库中的字符串值分配给图像的
ImageUrl
,但是
ImageUrl
始终为空,而
reader
对象在调试时不满足
while
循环条件

protected void Button3_Click(object sender, EventArgs e)
    {
        string query = "select profilePicture from Users where username = '@username'";
        command = new SqlCommand(query, connection);
        command.Connection = connection;
        command.Parameters.Add("@username", SqlDbType.VarChar).Value = User.Identity.Name;
        connection.Open();
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                ProfilePhotoButton.ImageUrl = reader[0].ToString();
            }
        }
        connection.Close();
    }

您的参数用单引号括起来。去掉那个

 string query = "select profilePicture from Users where username = '@username'";
                                                                  //HERE
应该是:

 string query = "select profilePicture from Users where username = @username";
将值括在单引号中将由您在向命令添加参数时指定的参数类型负责


作为旁注,请使用语句将您的命令和连接对象包含在
中,以确保资源的处置

参数用单引号括起来。去掉那个

 string query = "select profilePicture from Users where username = '@username'";
                                                                  //HERE
应该是:

 string query = "select profilePicture from Users where username = @username";
将值括在单引号中将由您在向命令添加参数时指定的参数类型负责


作为旁注,请使用
语句将您的命令和连接对象包含在
中,以确保资源的处置

参数用单引号括起来。去掉那个

 string query = "select profilePicture from Users where username = '@username'";
                                                                  //HERE
应该是:

 string query = "select profilePicture from Users where username = @username";
将值括在单引号中将由您在向命令添加参数时指定的参数类型负责


作为旁注,请使用
语句将您的命令和连接对象包含在
中,以确保资源的处置

参数用单引号括起来。去掉那个

 string query = "select profilePicture from Users where username = '@username'";
                                                                  //HERE
应该是:

 string query = "select profilePicture from Users where username = @username";
将值括在单引号中将由您在向命令添加参数时指定的参数类型负责

作为旁注,请使用
语句将您的命令和连接对象包含在
中,以确保资源的处置