//这是插入数据的代码
protected void Button1_Click(object sender, EventArgs e)
{
string ExName = string.Empty;
#region
if (FileSelect.PostedFile.FileName != null)
{
int filesize = FileSelect.PostedFile.ContentLength;//get the file of size
if (filesize > (500 * 1024) || filesize < 0)
{
Response.Write("<scirpt language='javascript'>alert('请确认图片存在且小于500K');</scirpt>");//大于500K或文件不存在时
}
else
{
try
{ //获取文件完整的路径及名称
string fullname = FileSelect.PostedFile.FileName.ToString();
//取得文件扩展名
ExName = fullname.Substring(fullname.LastIndexOf("."));
//一:利用时间生成新文件名(年+月+日+小时+分钟+秒+毫秒)
string NewName = DateTime.Now.Year.ToString() +
DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();
//二:利用GUID结构生成唯一文件名
Guid myguid = Guid.NewGuid();
NewName += myguid.ToString();
NewName = NewName.Replace("-","");
NewName = NewName.ToUpper();
//为新文件加上扩展名
NewName += ExName;
//HttpRuntime.AppDomainAppPath获取站点跟目录的物理路径
FileSelect.PostedFile.SaveAs(HttpRuntime.AppDomainAppPath + @"UpLoad\" + NewName);
//insert into sql server
string cmdsql = "data source=.;database=TryPicture;Trusted_Connection=yes";
SqlConnection conn = new SqlConnection(cmdsql);
conn.Open();
string cmdtext = "insert into Blog_Picture(pictureSize,pictureupload,pictureName,pictureDatetime,pictureType) values (@picturesize,@pictureupload,@pictureName,@pictureDatetime,@picturetype)";
SqlCommand comm = new SqlCommand(cmdtext, conn);
comm.Parameters.Add("pictureupload", SqlDbType.NVarChar).Value = @"upload/" + NewName;
comm.Parameters.Add("pictureName", SqlDbType.VarChar).Value = txtTitle.Text.Trim();
comm.Parameters.Add("pictureDatetime", SqlDbType.DateTime).Value = DateTime.Now.ToString();
comm.Parameters.Add("pictureType",SqlDbType.Char).Value=ExName;
comm.Parameters.Add("pictureSize",SqlDbType.Int).Value=filesize;
comm.ExecuteNonQuery();
conn.Close();
Response.Write("<script language='javascript'>alert('记录添加成功');</script>");
}
catch (Exception error)
{
Response.Write(error.ToString());
}
}
}
#endregion
}
在数据库中查询图片路径的结果是这样的路径 upload/20073122095EIFJISIA987.gif
//下面是读取的代码
private DataSet Get_Picture()
{
string cmdtext = "select pictureupload from Blog_Picture";
SqlConnection connget = new SqlConnection(cmdsql);
connget.Open();
SqlDataAdapter da = new SqlDataAdapter(cmdtext, connget);
DataSet ds = new DataSet();
da.Fill(ds,"picture");
return ds;
}
用image控件 怎么显示图片,根据数据集中取出的的图片路径啊?