protected void Button1_Click(object sender, EventArgs e)
{
if (this.FileUpload1.HasFile)
{
int i = this.FileUpload1.PostedFile.ContentLength; //得到上传文件大小
if (this.FileUpload1.PostedFile.ContentLength > 10485760) //1024*1024*10=10M,控制大小
{
Response.Write("<script>alert('文件不能超过10M !')</script>");
return;
}
string fileName = this.FileUpload1.FileName;
this.FileUpload1.PostedFile.SaveAs(Server.MapPath("~/") + "\\File\\" + fileName);//把文件上传到根目录的File文件夹中
SqlConnection myCon = new SqlConnection();
myCon.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
myCon.Open();
//2 实例化一个SqlCommand对象
SqlCommand myCom = new SqlCommand(); //3 利用修改命令赋值SqlCommand的CommandText
myCom.Connection = myCon;
myCom = "insert into file(fileLoad,fileName) Values('" + this.FileUpload1.PostedFile.SaveAs(Server.MapPath("~/") + "\\File\\" + fileName) + "','" + this.FileUpload1.FileName + "')"; //4 执行增加记录
myCom.ExecuteNonQuery(); //5 断开连接
myCon.Close(); Response.Write("<script language='javascript'>alert('上传文件成功!');location=myfileShare.aspx</script>"); }
}
{
if (this.FileUpload1.HasFile)
{
int i = this.FileUpload1.PostedFile.ContentLength; //得到上传文件大小
if (this.FileUpload1.PostedFile.ContentLength > 10485760) //1024*1024*10=10M,控制大小
{
Response.Write("<script>alert('文件不能超过10M !')</script>");
return;
}
string fileName = this.FileUpload1.FileName;
this.FileUpload1.PostedFile.SaveAs(Server.MapPath("~/") + "\\File\\" + fileName);//把文件上传到根目录的File文件夹中
SqlConnection myCon = new SqlConnection();
myCon.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
myCon.Open();
//2 实例化一个SqlCommand对象
SqlCommand myCom = new SqlCommand(); //3 利用修改命令赋值SqlCommand的CommandText
myCom.Connection = myCon;
myCom = "insert into file(fileLoad,fileName) Values('" + this.FileUpload1.PostedFile.SaveAs(Server.MapPath("~/") + "\\File\\" + fileName) + "','" + this.FileUpload1.FileName + "')"; //4 执行增加记录
myCom.ExecuteNonQuery(); //5 断开连接
myCon.Close(); Response.Write("<script language='javascript'>alert('上传文件成功!');location=myfileShare.aspx</script>"); }
}
事务开始:
{
string path=Server.MapPath("~/") + "\\File\\" + fileName);
this.FileUpload1.PostedFile.SaveAs(path);
myCom = "insert into file(fileLoad,fileName) Values('" + path+ "','" + this.FileUpload1.FileName + "')";
trans.Commit();
}
没返回值 所以累加不上。