⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addnews.cs

📁 这是一个用ASP加SQL做的新闻网站,里面有不少的新闻可以浏览,共享的.
💻 CS
字号:
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 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("new.mdb"));  
			   OleDbDataAdapter myCommand=new OleDbDataAdapter("SELECT id,typename FROM newstype ",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) 
	
		{  
			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>153600) 
			{ 
				Span1.Text="上传的文件不能超过70kb"; 
				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("new.mdb"));         
				OleDbCommand myCommand = new OleDbCommand("insert into news (biaoti, neirong,zuozhe,shijian,click,img,typeid) values ('" + biaoti.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("index.aspx") ; 
			}
	        
		}  
 
		public void reset_Click(Object sender,EventArgs e)
		{
			biaoti.Text="";
			neirong.Text="";
			zuozhe.Text="";
		}  

	} 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -