我正在使用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();
}
答案 0 :(得分:3)
您有单引号括起您的参数。删除它。
string query = "select profilePicture from Users where username = '@username'";
//HERE
应该是:
string query = "select profilePicture from Users where username = @username";
在为命令添加参数时,您在指定的参数类型中将使用单引号括起值。
作为旁注,请在using
语句中包含Command和Connection对象,以确保资源的处置。