//发布公告
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "" || this.textBox2.Text == "")
            {
                MessageBox.Show("请填写完整");
            }
            else
            {
                NewsModel newModel = new NewsModel();
                newModel.Title = this.textBox1.Text;
                newModel.Content = this.textBox2.Text;
                newModel.Pubtime = DateTime.Now.ToString();

                AddNewsBusiness addNewsBusiness = new AddNewsBusiness();
                bool result = false;
                result = addNewsBusiness.addnews(newModel);
                if (result == false)
                {
                    MessageBox.Show("添加失败,请重试!");
                }
                else
                {
                    MessageBox.Show("添加成功!");
                    this.Dispose();
                }
            }
        }
 public NewsModel[] getAllNews()
 {
     MySqlConnection conn = GetConn.getConn();
     try
     {
         conn.Open();
         NewsModel[] newsModelList;
         //int totalCount;
         //MySqlCommand cmd = new MySqlCommand("select count(*) from `tb_news`", conn);
         //totalCount = (int)cmd.ExecuteScalar();
         //newsModelList = new NewsModel[totalCount];
         //cmd.Dispose();
         MySqlCommand cmd = new MySqlCommand("select * from `tb_news` order by nid desc", conn);
         DataTable dt = new DataTable();
         MySqlDataAdapter da = new MySqlDataAdapter(cmd);
         //DataSet myDataSet = new DataSet();
         da.Fill(dt);
         int totalCount ;
         totalCount = dt.Rows.Count;
         newsModelList = new NewsModel[totalCount];
         int i = 0;
         for (; i < totalCount; i++)
         {
             newsModelList[i] = new NewsModel();
         }
         i = 0;
         foreach (DataRow myRow in dt.Rows)
         {
             newsModelList[i].Nid = int.Parse(myRow["nid"].ToString());
             newsModelList[i].Title = myRow["title"].ToString();
             newsModelList[i].Content = myRow["content"].ToString();
             newsModelList[i].Pubtime = myRow["pubtime"].ToString();
             i++;
         }
         //MySqlDataReader reader = cmd.ExecuteReader();
         //int i = 0;
         ////if (reader.Read())
         //{
         //    newsModelList[i].Nid = reader.GetInt32("nid");
         //    newsModelList[i].Title = reader.GetString("title");
         //    newsModelList[i].Content = reader.GetString("content");
         //    newsModelList[i].Pubtime = reader.GetString("pubtime");
         //    i++;
         //}
         conn.Close();
         return newsModelList;
     }
     catch (Exception)
     {
         conn.Close();
         return null;
     }
 }
 public FmNewsDetail(string formTitle,NewsModel newsModel,bool isEdit = false)
 {
     InitializeComponent();
     this.Text = formTitle;
     this.newsModel = newsModel;
     this.isEdit = isEdit;
     if (isEdit == true)
     {
         this.button1.Text = "保存";
         this.textBox1.ReadOnly = false;
         this.textBox2.ReadOnly = false;
     }
 }
 public bool addnews(NewsModel newsModel)
 {
     NewsService newsService = new NewsService();
     int nid = 0;
     nid = newsService.insert(newsModel);
     if (nid == 0)
     {
         return false;
     }
     else
     {
         return true;
     }
 }
 public bool updateNewsBusiness(NewsModel newsModel)
 {
     return new NewsService().update(newsModel);
 }
        public bool update(NewsModel newsModel)
        {
            string title = newsModel.Title;
            string pubtime = newsModel.Pubtime;
            string content = newsModel.Content;
            int nid = newsModel.Nid;

            MySqlConnection conn = GetConn.getConn();
            try
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand("update `tb_news` set title=@title,pubtime=@pubtime,content=@content where nid=@nid", conn);
                cmd.Parameters.AddWithValue("@title", title);
                cmd.Parameters.AddWithValue("@pubtime", pubtime);
                cmd.Parameters.AddWithValue("@content", content);
                cmd.Parameters.AddWithValue("@nid", nid);
                if (cmd.ExecuteNonQuery() == 1)
                {
                    cmd.Dispose();
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception)
            {
                conn.Close();
                return false ;
            }
        }
        public int insert(NewsModel newsModel)
        {
            string title = newsModel.Title;
            string pubtime = newsModel.Pubtime;
            string content = newsModel.Content;

            MySqlConnection conn = GetConn.getConn();
            try
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand("insert into `tb_news` (title,pubtime,content) values (@title,@pubtime,@content)", conn);
                cmd.Parameters.AddWithValue("@title", title);
                cmd.Parameters.AddWithValue("@pubtime", pubtime);
                cmd.Parameters.AddWithValue("@content", content);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                //返回主键值nid
                cmd = new MySqlCommand("select * from `tb_news` where `title`=@title", conn);
                cmd.Parameters.AddWithValue("@title", title);
                MySqlDataReader reader = cmd.ExecuteReader();
                int nid = 0;
                if (reader.Read())
                {
                    nid = int.Parse(reader["nid"].ToString());
                }
                conn.Close();
                return nid;
            }
            catch (Exception)
            {
                conn.Close();
                return 0;
            }
        }