上传文件出错:点击刷新后数据库中有两条相同的记录
在上传文件时出现了刷新页面后数据库中出现两天相同记录的情况,上传是通过一个button的CLICK事件触发的,下面是CS页面的具体代码,各位哥哥姐姐能告诉我是怎么回事么?
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class saveImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile upFile = up_file.PostedFile;
int FileLength = upFile.ContentLength;
try
{
if (FileLength==0)
{
Label1.Text = "<b>你还没有选择上传图片</b>";
}
else
{
Byte[] FileByteArray=new Byte[FileLength];
Stream streamobj=upFile.InputStream;
streamobj.Read(FileByteArray,0,FileLength);
string connstr=System.Configuration.ConfigurationManager.AppSettings["ConnectionSqlServer"];
string query="INSERT INTO ImageTable (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@ImageData, @ImageContentType, @ImageDescription, @ImageSize)";
SqlConnection conn=new SqlConnection(connstr);
SqlCommand cmd=new SqlCommand(query,conn);
cmd.Parameters.Add("@ImageData",SqlDbType.Image);
cmd.Parameters.Add("@ImageContentType",SqlDbType.VarChar,50);
cmd.Parameters.Add("@ImageDescription",SqlDbType.VarChar,200);
cmd.Parameters.Add("@ImageSize",SqlDbType.BigInt);
cmd.Parameters["@ImageData"].Value=FileByteArray;
cmd.Parameters["@ImageContentType"].Value=upFile.ContentType;
cmd.Parameters["@ImageDescription"].Value=Label1.Text;
cmd.Parameters["@ImageSize"].Value=FileLength;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
Label1.Text="<b>图片上传成功</b>";
}
}
catch(Exception ex)
{
Label1.Text=ex.Message.ToString();
}
}
}
------解决方案--------------------如果添加成功后还要在当前页:
1.重定向到该页
2.使用一个计时器,如果提交过就不在执行Button1_Click里的代码
否则跳转到别的页面去