protected void Button1_Click(object sender, EventArgs e)
{
try
{
String imgPath = FileUpload1.PostedFile.FileName;
String imgName = imgPath.Substring(imgPath.LastIndexOf("\\")+1);
String imgExtend = imgPath.Substring(imgPath.LastIndexOf(".")+1);
int FileLen = this.FileUpload1.PostedFile.ContentLength;
Byte[] FileData=new Byte[FileLen];
HttpPostedFile hp = FileUpload1.PostedFile;
Stream sr = hp.InputStream;
sr.Read(FileData,0,FileLen);
SqlConnection con = new SqlConnection("server=(local);user id=zheng;pwd=zheng;database=aa");
con.Open();
SqlCommand com = new SqlCommand("insert into Files(FileData)values(@imgdata)",con);
com.Parameters.Add("@imgdata",SqlDbType.Image);
com.Parameters["@imgdata"].Value = FileData;
com.ExecuteNonQuery();
}
catch(Exception error)
{
Response.Write("上传失败"+error.ToString());
}
{
try
{
String imgPath = FileUpload1.PostedFile.FileName;
String imgName = imgPath.Substring(imgPath.LastIndexOf("\\")+1);
String imgExtend = imgPath.Substring(imgPath.LastIndexOf(".")+1);
int FileLen = this.FileUpload1.PostedFile.ContentLength;
Byte[] FileData=new Byte[FileLen];
HttpPostedFile hp = FileUpload1.PostedFile;
Stream sr = hp.InputStream;
sr.Read(FileData,0,FileLen);
SqlConnection con = new SqlConnection("server=(local);user id=zheng;pwd=zheng;database=aa");
con.Open();
SqlCommand com = new SqlCommand("insert into Files(FileData)values(@imgdata)",con);
com.Parameters.Add("@imgdata",SqlDbType.Image);
com.Parameters["@imgdata"].Value = FileData;
com.ExecuteNonQuery();
}
catch(Exception error)
{
Response.Write("上传失败"+error.ToString());
}
}
有错误大家指正..