【问题标题】:Default profile picture in sql server using varbinary(max)使用 varbinary(max) 在 sql server 中的默认配置文件图片
【发布时间】:2013-07-28 18:22:18
【问题描述】:

我有一个包含用户个人资料页面的 Web 应用程序。该页面允许用户上传自己的个人资料图片。我正在寻找一种方法来为我在数据库(SQL Server 2008)中的个人资料图片设置默认的varbinary(max) 值。像 Facebook 这样的东西,一旦你注册就会有一个默认的个人资料图片。我可以将图片上传到数据库并显示出来,但如果我在数据库中的个人资料图片列是NULL,我会收到错误解码图像。

上传图片代码:

string filePath = FileUpload1.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);

string contenttype = String.Empty;

switch (ext)
{
    case ".jpg":
       contenttype = "image/jpg";
       break;
}

if (contenttype != String.Empty)
{
   System.Drawing.Image uploaded = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);

   System.Drawing.Image newImage = new Bitmap(1024, 768);

   using (Graphics g = Graphics.FromImage(newImage))
   {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage(uploaded, 0, 0, 1024, 768);
   }

   byte[] results;

   using (MemoryStream ms = new MemoryStream())
   {
         ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
         EncoderParameters jpegParms = new EncoderParameters(1);
         jpegParms.Param[0] = new EncoderParameter(Encoder.Quality, 95L);
         newImage.Save(ms, codec, jpegParms);
         results = ms.ToArray();
   }

显示图片代码:

string strQuery = "select profilepic from MemberAccount where nric='"+ Session["nric"] +"'";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
if (!dt.Rows[0]["profilepicture"].Equals(DBNull.Value))
{
   download(dt);
}

private DataTable GetData(SqlCommand cmd)
{
    DataTable dt = new DataTable();
    SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI");
    SqlDataAdapter sda = new SqlDataAdapter();
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    try
    {
       con.Open();
       sda.SelectCommand = cmd;
       sda.Fill(dt);
       return dt;
    }
    catch
    {
       return null;
    }
    finally
    {
        con.Close();
        sda.Dispose();
        con.Dispose();
    }
}


private void download(DataTable dt)
{
    Byte[] bytes = (Byte[])dt.Rows[0]["profilepicture"];
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "image/jpg";
    Response.BinaryWrite(bytes);
    Response.End();
}

在显示图片但有错误的部分添加if语句

if (!dt.Rows[0]["profilepicture"].Equals(DBNull.Value))

这一行给出错误:

Object reference not set to an instance of an object.

【问题讨论】:

  • 你可以检查dt.Rows[0]["profilepicture"]是否为空。你能添加你得到的异常吗?
  • 我尝试使用这个if (!dt.Rows[0]["profilepicture"].Equals(null)),但它不起作用
  • 您能否更新一下您如何从数据库中检索数据的问题,dt 是什么?
  • 这个问题的答案是stackoverflow.com/questions/4604414/…。你必须检查DBNull.Value。
  • @Damith dt 是数据表。

标签: c# sql-server varbinarymax


【解决方案1】:

为什么不将默认图像保存在您网站上的某些图像文件夹中? 这样您就不必读取数据库列,而是使用图像控件中的相对路径。

尝试比较

if(!dt.Rows[0]["profilepicture"].Equals(DBNull.Value))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多