新闻管理数据模板

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using domain.SQLDB;
namespace domain.SQLDB
{

public class News{
private string _Gid;
private string _TypeId;
private string _Title;
private string _Keyword;
private string _ComeFrom;
private string _Author;
private string _Content;
private string _Pic;
private string _IsCommand;
private string _commandTime;
private string _IsTop;
private string _topTime;
private string _AddTime;
public string Gid{set{this._Gid=value;}get{return this._Gid;}}

public string TypeId { set { this._TypeId = value; } get { return this._TypeId; } }
public string Title { set { this._Title = value; } get { return this._Title; } }
    public string Keyword { set { this._Keyword = value; } get { return this._Keyword; } }
public string ComeFrom{set{this._ComeFrom=value;}get{return this._ComeFrom;}}
public string Author{set{this._Author=value;}get{return this._Author;}}
public string Content{set{this._Content=value;}get{return this._Content;}}
public string Pic{set{this._Pic=value;}get{return this._Pic;}}
public string IsCommand{set{this._IsCommand=value;}get{return this._IsCommand;}}
public string commandTime{set{this._commandTime=value;}get{return this._commandTime;}}
public string IsTop{set{this._IsTop=value;}get{return this._IsTop;}}
public string topTime{set{this._topTime=value;}get{return this._topTime;}}
public string AddTime{set{this._AddTime=value;}get{return this._AddTime;}}

DbAccess db = new DbAccess();
public bool Add(){
string sql = "insert into News([Gid],[Title],[ComeFrom],[Author],[Content],[Pic],[IsCommand],[commandTime],[IsTop],[topTime],[AddTime]) values(@Gid,@Title,@ComeFrom,@Author,@Content,@Pic,@IsCommand,@commandTime,@IsTop,@topTime,@AddTime)";
SqlParameter[] pas = {new SqlParameter("@Gid",Gid),new SqlParameter("@Title",Title),new SqlParameter("@ComeFrom",ComeFrom),new SqlParameter("@Author",Author),new SqlParameter("@Content",Content),new SqlParameter("@Pic",Pic),new SqlParameter("@IsCommand",IsCommand),new SqlParameter("@commandTime",commandTime),new SqlParameter("@IsTop",IsTop),new SqlParameter("@topTime",topTime),new SqlParameter("@AddTime",AddTime)};
return db.GetState(sql,pas);
}

public bool Update()
{
    string sql = "if exists(select Gid from News where Gid=@Gid) begin  update News set [TypeId]=@TypeId,[Title]=@Title,[Keyword]=@keyword,[ComeFrom]=@ComeFrom,[Author]=@Author,[Content]=@Content,[Pic]=case when len(@Pic)>0 then @Pic else Pic end,[IsCommand]=@IsCommand,[commandTime]=@commandTime,[IsTop]=@IsTop,[topTime]=@topTime where Gid=@Gid end else begin insert into News([TypeId],[Title],[keyword],[ComeFrom],[Author],[Content],[Pic],[IsCommand],[commandTime],[IsTop],[topTime]) values(@TypeId,@Title,@keyword,@ComeFrom,@Author,@Content,@Pic,@IsCommand,@commandTime,@IsTop,@topTime) end";
    SqlParameter[] pas = { new SqlParameter("@Gid", Gid), new SqlParameter("@TypeId", TypeId), new SqlParameter("@Title", Title), new SqlParameter("@keyword", Keyword), new SqlParameter("@ComeFrom", ComeFrom), new SqlParameter("@Author", Author), new SqlParameter("@Content", Content), new SqlParameter("@Pic", Pic), new SqlParameter("@IsCommand", IsCommand), new SqlParameter("@commandTime", commandTime), new SqlParameter("@IsTop", IsTop), new SqlParameter("@topTime", topTime) };
return db.GetState(sql,pas);
}

public bool Delete()
{string sql = "delete News where [Gid]=@Gid";
SqlParameter[] pas = {new SqlParameter("@Gid",Gid)};
return db.GetState(sql,pas);
}
public DataTable Select()
{
DataTable dt = db.GetTable("select * from News")??new DataTable();
return dt;
}
//查询
public News SelectById(){
News  news = new News();
string sql = "select * from News where Gid=@Gid";
SqlParameter[] pas = {new SqlParameter("@Gid",Gid)};
DataTable dt = db.GetTable(sql,pas);
  if(dt.Rows.Count>0)
    {
      news.Gid=Convert.ToString(dt.Rows[0]["Gid"]);
      news.TypeId = Convert.ToString(dt.Rows[0]["TypeId"]);
      news.Title=Convert.ToString(dt.Rows[0]["Title"]);
      news.Keyword = Convert.ToString(dt.Rows[0]["Keyword"]);     
      news.ComeFrom=Convert.ToString(dt.Rows[0]["ComeFrom"]);
      news.Author=Convert.ToString(dt.Rows[0]["Author"]);
      news.Content=Convert.ToString(dt.Rows[0]["Content"]);
      news.Pic=Convert.ToString(dt.Rows[0]["Pic"]);
      news.IsCommand=Convert.ToString(dt.Rows[0]["IsCommand"]);
      news.commandTime=Convert.ToString(dt.Rows[0]["commandTime"]);
      news.IsTop=Convert.ToString(dt.Rows[0]["IsTop"]);
      news.topTime=Convert.ToString(dt.Rows[0]["topTime"]);
      news.AddTime=Convert.ToString(dt.Rows[0]["AddTime"]);

    }
return news;
}

}
}

你可能感兴趣的:(sql)