我的 sql 数据库中有图像表,其中包含三列名称 id、名称和图像。当我尝试检索图像时,它在第一行显示错误:
“你调用的对象是空的。”
我想查看下拉列表中的图像名称和图像控件中的图像名称。
SqlConnection con = new SqlConnection("Data Source=LOCALHOST\\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=True");
//SqlConnection con = new SqlConnection("Data Source=(localdb)\v11.0;Initial Catalog=tempdb;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
SqlCommand cm = new SqlCommand("select * from image where name='" + DropDownList1.SelectedItem.ToString() + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cm);
SqlDataReader dr = cm.ExecuteReader();
try
{
if (dr.Read())
{
string image1 = Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs1 = new FileStream(image1, FileMode.CreateNew, FileAccess.Write);
byte[] bimage1 = (byte[])dr["name"];
fs1.Write(bimage1, 0, bimage1.Length - 1);
fs1.Flush();
Image1.ImageUrl = "~/Images/" + DropDownList1.SelectedItem.ToString();
Image1.Visible = true;
}
dr.Close();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
}