addnews2.cs
资源名称:Visual.rar [点击查看]
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:3k
源码类别:
C#编程
开发平台:
Others
- using System;
- using System.Data;
- using System.Data.OleDb;
- using System.IO;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- namespace www
- {
- public class MyCodeBehind : Page
- {
- public System.Web.UI.WebControls.DropDownList DropDownList2;
- public System.Web.UI.WebControls.TextBox biaoti;
- public System.Web.UI.WebControls.TextBox yinzi;
- public System.Web.UI.WebControls.TextBox neirong;
- public System.Web.UI.WebControls.TextBox zuozhe;
- public System.Web.UI.WebControls.Label Label1;
- public System.Web.UI.WebControls.Label Span1;
- public HtmlInputFile File1;
- public HtmlInputButton Button1;
- void Page_Load(Object sender, EventArgs e)
- {
- if(!IsPostBack)
- {
- OleDbConnection MyConnection =new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("candsql.mdb"));
- OleDbDataAdapter myCommand=new OleDbDataAdapter("SELECT id,typename FROM bb ",MyConnection);
- DataSet ds= new DataSet();
- myCommand.Fill(ds,"bb");
- DropDownList2.DataSource = ds.Tables["bb"].DefaultView;
- DropDownList2.DataTextField = "typename";
- DropDownList2.DataValueField = "id";
- DropDownList2.DataBind();
- }
- }
- public void Button1_Click(object Source, EventArgs e)
- {
- //创建一个上传文件路径字符串 把所有上传文件 存储到file文件夹中
- string filepath = Server.MapPath("file/"+Path.GetFileName (File1.PostedFile.FileName));
- if ((biaoti.Text=="")||(neirong.Text=="")||(zuozhe.Text==""))
- {
- Label1.Text="标题、内容、作者等不能为空!";
- }
- else if (biaoti.Text.Length>=50)
- {
- Label1.Text="你的标题太长了!";
- }
- //这里也可以上传别的文件 (不只是图片文件)
- //检查上传文件大小
- else if (File1.PostedFile.ContentLength>473600)
- {
- Span1.Text="上传的文件不能超过400kb";
- return;
- }
- //检查是否重名
- else if (File.Exists(filepath))
- {
- Span1.Text="上传文件重名,请改名后再上传!";
- return;
- }
- else
- {
- if(File1.PostedFile != null)
- try
- {
- File1.PostedFile.SaveAs(filepath);
- }
- catch (Exception exc)
- {
- Span1.Text = "保存文件时出错<b>" + filepath + "</b><br>"+ exc.ToString();
- }
- OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ Server.MapPath("candsql.mdb"));
- OleDbCommand myCommand = new OleDbCommand("insert into aa (biaoti,yinzi,neirong,zuozhe,shijian,click,img,typeid) values ('" + biaoti.Text.ToString() + "','" + yinzi.Text.ToString() + "', '" + neirong.Text.ToString() + "','" + zuozhe.Text.ToString() + "','" + DateTime.Now.ToString() + "',0,'" + Path.GetFileName(File1.PostedFile.FileName) + "','" + DropDownList2.SelectedItem.Value+ "')" , myConnection);
- myCommand.Connection.Open();
- myCommand.ExecuteNonQuery();
- myCommand.Connection.Close();
- Response.Redirect("morenews4.aspx") ;
- }
- }
- public void reset_Click(Object sender,EventArgs e)
- {
- biaoti.Text="";
- neirong.Text="";
- zuozhe.Text="";
- yinzi.Text="";
- }
- }
- }