ASP中如何从SQL Server数据库中检索图像.. NET使用c#
本文关键字:图像 检索 NET 使用 数据库 Server SQL ASP | 更新日期: 2023-09-27 18:01:39
我在SQL Server数据库中有一些图像,我想在ASP.NET中检索这些图像。但是它给了我一个错误
文件"C:'Program Files'Common Files'Microsoft .共享'DevServer'10.0'130065367816821657'已存在
请解决我的问题。
protected void DropDownList1_TextChanged(object sender, EventArgs e)
{
cn.Open();
SqlCommand cm = new SqlCommand("select * from imageCollection where img_id='" + DropDownList1.SelectedItem.ToString() + "'", cn);
SqlDataAdapter da = new SqlDataAdapter(cm);
SqlDataReader dr = cm.ExecuteReader();
try
{
if (dr.Read())
{
string image1 = Convert.ToString(DateTime.Now.ToFileTime());
string image2 = Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs1 = new FileStream(image1, FileMode.CreateNew, FileAccess.Write);
FileStream fs2 = new FileStream(image2, FileMode.CreateNew, FileAccess.Write);
byte[] bimage1 = (byte[])dr["passport_photo"];
byte[] bimage2 = (byte[])dr["sign_photo"];
fs1.Write(bimage1, 0, bimage1.Length - 1);
fs2.Write(bimage2, 0, bimage2.Length - 1);
fs1.Flush();
fs2.Flush();
Image1.ImageUrl = "~/images"+bimage1.ToString();
Image2.ImageUrl = "~/images"+bimage2.ToString();
}
dr.Close();
cn.Close();
}
catch (Exception ex)
{
throw ex;
}
我已经从"C:'Program Files'Common Files'microsoft shared'DevServer'10.0"上传了图像,前端代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
string image1 = FileUpload1.FileName;
string image2 = FileUpload2.FileName;
FileStream fs1 = new FileStream(image1, FileMode.Open, FileAccess.Read);
FileStream fs2 = new FileStream(image2, FileMode.Open, FileAccess.Read);
byte[] bimage1 = new byte[fs1.Length];
byte[] bimage2 = new byte[fs2.Length];
fs1.Read(bimage1, 0, Convert.ToInt32(fs1.Length));
fs2.Read(bimage2, 0, Convert.ToInt32(fs2.Length));
fs1.Close();
fs2.Close();
cn.Open();
SqlParameter sp = new SqlParameter();
sp.SqlDbType = SqlDbType.Image;
sp.ParameterName = "@passport_photo";
sp.ParameterName = "@sign_photo";
sp.Value = bimage1;
sp.Value = bimage2;
SqlCommand cm = new SqlCommand("INSERT INTO imageCollection values(@img_id," + "@passport_photo,"+"@sign_photo)", cn);
cm.Parameters.AddWithValue("@img_id",TextBox1.Text);
cm.Parameters.AddWithValue("@passport_photo",sp.Value=bimage1);
cm.Parameters.AddWithValue("@sign_photo",sp.Value=bimage2);
cm.ExecuteNonQuery();
cm.Dispose();
cn.Dispose();
cn.Close();
}
}
你的问题是image1
和image2
是相等的。
string image1 = Convert.ToString(DateTime.Now.ToFileTime());
string image2 = Convert.ToString(DateTime.Now.ToFileTime());
考虑使用一些东西来区分两个文件名:
string image1 = Convert.ToString(DateTime.Now.ToFileTime()) + "1";
string image2 = Convert.ToString(DateTime.Now.ToFileTime()) + "2";
您可以想象,创建文件名Convert.ToString(DateTime.Now.ToFileTime())
所涉及的代码执行得非常快,因此没有足够的时间让DateTime.Now
增加其值。
编辑
您也可以更改写入图像的目录:
FileStream fs1 = new FileStream(Server.MapPath("~/images/" + image1), FileMode.CreateNew, FileAccess.Write);
FileStream fs2 = new FileStream(Server.MapPath("~/images/" + image2), FileMode.CreateNew, FileAccess.Write);
否则,文件将写入应用程序文件夹(bin
)
和
Image1.ImageUrl = "~/images/" + image1;
Image2.ImageUrl = "~/images/" + image2;
现在有意义了
确保ASP。. NET用户对"images"文件夹有写权限
另外,您的代码被设置为在找到具有重复文件名的文件时抛出错误:
FileStream fs1 = new FileStream(image1, FileMode.CreateNew, FileAccess.Write);
FileStream fs2 = new FileStream(image2, FileMode.CreateNew, FileAccess.Write);
下面的代码将允许覆盖一个文件,通过将FileMode.CreateNew
更改为FileMode.Create
:
FileStream fs1 = new FileStream(image1, FileMode.Create, FileAccess.Write);
FileStream fs2 = new FileStream(image2, FileMode.Create, FileAccess.Write);