显示相应路径的图像,保存在数据库中

时间:2014-05-08 13:15:31

标签: 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.Substring(0,Application.StartupPath.LastIndexOf("bin"));
    Bitmap imgImage = new Bitmap(pictureBox1.Image); 
    imgImage.Save(path + "\\Image\\" + picname + ".jpg");
    location = path + "'\'Image'\'" + picname;
}
else
    location = "";

我的问题是如何在相应的路径中显示图片框中的图像,该路径保存在数据库中。在此代码中,字符串变量位置保存在数据库中。我使用的是SQL服务器。提前谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

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;
}