C# 显示来自其相应路径的图像,该路径保存在数据库中

C# 显示来自其相应路径的图像,该路径保存在数据库中,c#,sql,image,visual-studio-2010,C#,Sql,Image,Visual Studio 2010,在我的应用程序中,我已将图像存储在文件夹中,并使用以下代码将其相应路径保存在数据库中: if (pictureBox1.Image != null) { string imagepath = pictureBox1.ImageLocation.ToString(); string picname = imagepath.Substring(imagepath.LastIndexOf('\\')); string path = Application.StartupPath

在我的应用程序中,我已将图像存储在文件夹中,并使用以下代码将其相应路径保存在数据库中:

if (pictureBox1.Image != null)
{
    string imagepath = pictureBox1.ImageLocation.ToString();
    string picname = imagepath.Substring(imagepath.LastIndexOf('\\'));
    string path = Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("bin"));
    Bitmap imgImage = new Bitmap(pictureBox1.Image); 
    imgImage.Save(path + "\\Image\\" + picname + ".jpg");
    location = path + "'\'Image'\'" + picname;
}
else
    location = "";
我的问题是如何在picturebox中显示保存在数据库中的相应路径中的图像。在这段代码中,字符串变量位置保存在数据库中。我正在使用SQL server。提前感谢。

试试这个:

string ImagePath = "";

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);

SqlCommand cmd = new SqlCommand("SELECT photo FROM othersInfo WHERE empID='" + s + "'", con);
cmd.CommandType = CommandType.Text;

con.Open();

SqlDataReader dataReader = cmd.ExecuteReader();

if(dataReader.HasRows)
{
  dataReader.Read();
  ImagePath = Convert.ToString(dataReader["photo"]);
}

if (ImagePath != "") 
{
  Image image = Image.FromFile(ImagePath)
  pictureBox1.Image = image;
} 

检查此链接此链接无效。每当我试图查看图像时,它都会给出错误信息“未处理的异常”。什么样的异常?viewProfile vp=新viewProfile;字符串pic=从其他信息中选择照片,其中empID='+s+';如果是图片!={vp.picViewProfile.ImageLocation=pic;}否则vp.picViewProfile.Image=null;此代码在图片框中显示错误imahe。我也用同样的查询@Bayeni尝试了你的代码,但到目前为止没有成功。我编辑了我的代码,为你提供了一个完整的解决方案,你只需要给它一个连接字符串,它就可以工作了。