using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 System.Data.SqlClient;
using System.IO;public partial class G_Show : System.Web.UI.Page
{
      
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FileUpload1.Visible = false;
            DateTime now = DateTime.Now;
            shijian.Text = now.ToShortDateString();
            //fabuzhe.Text = Session["User"].ToString();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {        //链接SQL Server数据库
        string sql = System.Configuration.ConfigurationManager.AppSettings["strConn"].ToString();
        SqlConnection strConn = new SqlConnection(sql);
       
        if ((biaoti.Text == "") || (neirong.Text.ToString() == ""))
        {
            Label1.Text = "标题或内容不能为空!";
        }
        else if (biaoti.Text.Length >= 100)
        {
            Label1.Text = "你的标题太长了!";
        }
        else
        {
                       string  ConnStr = "insert into 公告表(标题,发布者,发布时间,内容) VALUES( '" + biaoti.Text + "','" + fabuzhe.Text + "','" + shijian.Text + "', '" + neirong.Text + "')";
            SqlCommand  MyCommand = new SqlCommand(ConnStr, strConn);
            strConn.Open();
            MyCommand.ExecuteNonQuery();
            Label1.Text = "增加成功!";
            if (FileUpload1.PostedFile .FileName .Trim ()!= null)
            {
                System.IO.Stream fileDataStream = FileUpload1.PostedFile.InputStream;                //得到文件大小
                int fileLength = FileUpload1.PostedFile.ContentLength;                //创建数组
                byte[] fileData = new byte[fileLength];
                //把文件流填充到数组
                fileDataStream.Read(fileData, 0, fileLength);
                //得到文件类型
                string fileType = FileUpload1.PostedFile.ContentType;
                string name = FileUpload1.PostedFile.FileName;
                int i = name.LastIndexOf(".");                string newext = name.Substring(i);
                //将新记录插入到数据库中
                //ConnStr = "insert into 公告表(标题,发布者,发布时间,内容,附件) VALUES( '" + biaoti.Text + "','" + fabuzhe.Text + "','" + shijian.Text + "', '" + neirong.Text + "','" + fileData + "')";
                //        MyCommand = new SqlCommand(ConnStr, strConn);
                //        MyCommand.ExecuteNonQuery();
                //        Label1.Text = "增加成功!";
                SqlCommand command = new SqlCommand("INSERT INTO 公告表 (标题,发布者,发布时间,内容,附件,附件类型)" + "VALUES (@标题,@发布者,@发布时间,@内容,@附件,@附件类型)", strConn);                command.Parameters.AddWithValue("@标题", biaoti.Text);
                command.Parameters.AddWithValue("@发布者", fabuzhe.Text);
                command.Parameters.AddWithValue("@发布时间", shijian.Text);
                command.Parameters.AddWithValue("@内容", neirong.Text);
                command.Parameters.AddWithValue("@附件", fileData);
                command.Parameters.AddWithValue("@附件类型", newext);
                
                command.ExecuteNonQuery();
                Label1.Text = "成功";
            }
            biaoti.Text = "";
            neirong.Text = "";        }            }    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("G_list.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        FileUpload1.Visible = true;
    }
}