数据不能更新
using System;using System.Collections;
using
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using
using System.Data.SqlClient;
public partial class Admin_Admin_Edit : System.Web.UI.Page
{
private Int32 FileLength = 0;
OleDbConnection MyConn;
protected void Page_Load(object sender, EventArgs e)
{
MyConn=DB.CreateDB();
MyConn.Open();
if (Session["UserID"] == "" || Session["UserID"] == null)
{
Response.Write("<script language=javascript>window.alert('为了系统安全,请您重新登陆');window.location.href=('Admin_Login.aspx')</script>");
}
try
{
string id= Request.QueryString["id"].ToString();
string ww="select * from product where id="+id+"";
OleDbCommand Cm = new OleDbCommand(ww, MyConn);
OleDbDataReader dr=Cm.ExecuteReader();
if(dr.Read())
{
this.TxtTitle.Text=dr["title"].ToString();
this.TxtContent.Text=dr["content"].ToString();
this.label.Text=dr["img"].ToString();
}
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "')</script>");
}
}
protected void BtnnSubmit_Click(object sender, EventArgs e)
{
MyConn=DB.CreateDB();
MyConn.Open();
{
try
{
string id= Request.QueryString["id"].ToString();
string title =FunStr(this.TxtTitle.Text);
string content =FunStr(this.TxtContent.Text);
string FileName = myFile.Value;
HttpPostedFile UpFile = myFile.PostedFile; //获取对由客户端指定的上传文件的访问
FileLength = UpFile.ContentLength;//获取上传文件的字节大小
if (FileLength == 0)
{
OleDbCommand cmd1 = new OleDbCommand("update product set title="+title+",content="+content+" where id="+id+"",MyConn);
cmd1.ExecuteNonQuery();
Response.Write("<script>alert('数据更新成功')</script>");
}
else
{
string exName = FileName.Substring(FileName.LastIndexOf(".") + 1).ToUpper();//截取图片的后缀名
if (exName == "JPG" || exName == "BMP" || exName == "GIF")//判断图片的类型
{
if (FileLength > 204800)//判断图片是否大于200k(根据自己的需要判断大小)
{
Response.Write("<script>alert('对不起,图片大小不能大于200K')</script>");
return;
}
else
{
string ImageName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + "." + exName;//图片名称设置为保存的时间
Byte[] FileByte = new Byte[FileLength];
Stream ObjectStream = UpFile.InputStream;
ObjectStream.Read(FileByte, 0, FileLength); //读取图象文件数据
string StrSql = "update product set img="+ImageName+", title="+title+",content="+content+" where id="+id+"";
OleDbCommand Cmd = new OleDbCommand(StrSql, MyConn);
//Cmd.Parameters.Add("@Image",OleDbType.Binary, FileLength).Value = FileByte;
Cmd.Parameters.Add("@ImageName", OleDbType.VarChar, 100).Value = ImageName;
Cmd.ExecuteNonQuery();
Response.Write("<script>alert('数据更新成功')</script>");
}
}
else
{
Response.Write("<script>alert('对不起,请选择正确的的图片!')</script>");
return;
}
}
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "')</script>");
}
}
}
public static string FunStr(string str)
{
str = str.Replace("'", "‘");
str = str.Replace(" ", " ");
str = str.Trim();
if (str.Trim().ToString() == "")
str = "无";
return str;
}
}