欧美日韩一区二区三区四区不卡,日韩欧美视频一区二区三区四区,久久精品欧美一区二区三区不卡,国产精品久久久乱弄

咨詢電話:186 7916 6165 咨詢電話:186 7916 6165 (微信同號)    在線QQ:181796286
NEWS BLOG ·
學無止境
關注開優(yōu)網(wǎng)絡 關注前沿
ASP.NET常用服務器控件方法及屬性
ASP.NET 關鍵詞keywords與description的設置

ASP.NET Web 開發(fā)之代碼生成及增、刪、改

發(fā)表日期:2015-12-02    文章編輯:南昌開優(yōu)網(wǎng)絡    瀏覽次數(shù):4581    標簽:ASP.NET應用

配置2.75動軟生成器
工具 → 選項 → 基本設置 → 頂級命名空間 → Xiaobin.Shop
數(shù)據(jù)訪問基類 → DbHelperSQL
項目名稱 → demo_
存儲過程前綴 → up_
類命名規(guī)則  首字母大寫  DAL → 表名+DAL
選擇默認代碼模板 DAL → BuilderDALELParam  //基于企業(yè)庫param方式
批量生成代碼時去掉數(shù)據(jù)表的前綴如:shop_


微軟企業(yè)庫配置Web.confing文件  .net3.5采用Enterprise Library 4.1- October 2008  .net4.0采用 Enterprise Library 5.0
File → open → 找到項目中的web.config文件 → 
Database Sottings → LocalSqlServer →   //連接的數(shù)據(jù)庫
Namae → XiaobinShopConnStr →    //連接的名稱
Database → System.Data.SqlClient → //數(shù)據(jù)庫的類型
Connection → server=.\sqlexpress;database=xiaobinshop;uid=sa;pwd=123456; → //連接地址
Database Sottings → Default → XiaobinShopConnStr →   //默認連接地名稱

項目中的DAL層中需要引用微軟企業(yè)庫安裝文件夾中bin文件夾中的
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Data.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Interception.dll

windows身份驗證連接數(shù)據(jù)庫
<add name="XiaobinCMSConnStr" connectionString ="Integrated Security=sspi; server=.\sqlexpress;database=cms;connect timeout=30"  providerName="System.Data.SqlClient"/>
動軟自動生成代碼修改的地方:
//分頁  //fields選擇的字段  order排序的字段  ordertype排序方式  PageSize頁大小  PageIndex頁索引  strWhere條件
public DataSet GetList(string fields, string order, string ordertype, int PageSize, int PageIndex, string strWhere)
{
Database db = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = db.GetStoredProcCommand("proc_SplitPage");
db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "bbs_log");
db.AddInParameter(dbCommand, "strFields", DbType.AnsiString, fields);
db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
db.AddInParameter(dbCommand, "strOrder", DbType.String, order);
db.AddInParameter(dbCommand, "strOrderType", DbType.String, ordertype);
db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
return db.ExecuteDataSet(dbCommand);
}


///Model分頁
public List<Xiaobin.BBS.Model.Log> GetListArray(string fields, string order, string ordertype, int PageSize, int PageIndex, string strWhere)
{
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("proc_SplitPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "bbs_log");
            db.AddInParameter(dbCommand, "strFields", DbType.AnsiString, fields);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "strOrder", DbType.String, order);
            db.AddInParameter(dbCommand, "strOrderType", DbType.String, ordertype);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            List<Xiaobin.BBS.Model.Log> list = new List<Xiaobin.BBS.Model.Log>();          
            using (IDataReader dataReader = db.ExecuteReader(dbCommand)
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
}


///計算記錄數(shù)
  public int CalcCount(string strWhere)
        {
            string sql = "select count(1) from bbs_log";
            if (!string.IsNullOrEmpty(strWhere))
            {
                sql += " where " + strWhere;
            }
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(sql);
            return int.Parse(db.ExecuteScalar(dbCommand).ToString());
        }


//cs.文件執(zhí)行增、刪、改、查 在admin后臺中在同一頁面中
//頁面加載
DAL.LinkDAL dao = new DAL.LinkDAL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                anp.RecordCount = dao.CalcCount(GetCond());
                BindRep();
            }
        }
        //綁定列表
        private void BindRep()
        {
            rep.DataSource = dao.GetList("*", "createdate", "desc", anp.PageSize, anp.CurrentPageIndex, GetCond());
            rep.DataBind();
        }
        //獲取條件
        private string GetCond()
        {
            string cond = "";

            if (txtkey.Text.Length != 0)
            {
                string key = txtkey.Text.Trim();
                key = Utility.Tool.GetSafeSQL(key); //過濾非法字符
                string tmp = ddltype.SelectedValue;
                if (tmp == "linkname")
                {
                    cond = " linkname  like '%" + key + "%'";
                }
                else if (tmp == "linkurl")
                {
                    cond = " linkurl like '%" + key + "%'";
                }
            }
            return cond;
        }

       //多條件搜索
      如果是ddl控件時 前臺的DropDownList控件 加上AutoPostBack="true" 
      private string GetCond()
        {
           string cond="type='優(yōu)惠活動'";
          if (txtkey.Text.Length != 0)
            {
                string key = txtkey.Text.Trim();
                key = Utility.Tool.GetSafeSQL(key); //過濾非法字符
                string tmp = ddltype.SelectedValue;    //條件為ddl選中的值
                if (tmp == "linkname")
                {
                    cond += " linkname  like '%" + key + "%'";
                }               
            }
            return cond;
        } 

        //通過傳入type值獲取條件實現(xiàn)不同的綁定 在Page_load中還是和上面一樣
        private string GetCond()
{
    string cond = "1=1";
    string type = Request.QueryString["type"];
    if (!string.IsNullOrEmpty(type))
    {
        cond += " and type=" + type + "";
    }
    if (txtkey.Text.Trim().Length != 0)
    {
        string key = txtkey.Text.Trim();
        key = Xiaobin.Utility.Tool.GetSafeSQL(key);
        cond += " and title like '%" + key + "%' or keyword like '%" + key + "%' ";
    }
    return cond;
}
        
        //分頁
        protected void anp_PageChanged(object sender, EventArgs e)
        {
            BindRep();
        }
        //添加
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string linkname = txtnmae.Text.Trim();
            string linkurl = txturl.Text.Trim();

            if (linkname.Length == 0 || linkurl.Length == 0)
            {
                Utility.Tool.Alert("請輸入完整再提交", this.Page);
                return;
            }

            if (hfOpreate.Value == "add")
            {
                int res = dao.Add(new Xiaobin.Shop.Model.Link()
                {
                    linkname = linkname,
                    linkurl = linkurl,
                });
                if (res > 0)
                {
                    Utility.Tool.AlertAndGo("添加成功", Request.Url.ToString(), this.Page);
                    return;
                }
                else
                {
                    Utility.Tool.Alert("添加失敗,請聯(lián)系管理員", this.Page);
                    return;
                }
            }
            else if (hfOpreate.Value == "edit")
            {
                string id = hfid.Value;
                int x;
                if (int.TryParse(id, out x))
                {
                    Model.Link model = dao.GetModel(x);
                    if (model != null)
                    {
                        model.linkurl = linkurl;
                        model.linkname = linkname;
                    }
                    dao.Update(model);
                    Utility.Tool.AlertAndGo("修改成功", Request.Url.ToString(), this.Page);
                }
            }
        }
        //刪除
        protected void lbtnDel(object sender, EventArgs e)
        {
            string id = (sender as LinkButton).CommandArgument;
            dao.Delete(int.Parse(id));
            Utility.Tool.AlertAndGo("刪除成功", Request.Url.ToString(), this.Page);
        }

        //修改
        protected void lbtnMod(object sender, EventArgs e)
        {
            string id = (sender as LinkButton).CommandArgument;
            hfid.Value = id;
            hfOpreate.Value = "edit";
            Model.Link model = dao.GetModel(int.Parse(id));
            if (model != null)
            {
                txtnmae.Text = model.linkname;
                txturl.Text = model.linkurl;
                btnAdd.Text = "修改";
            }
        }
        //搜索
        protected void btnSo_Click(object sender, EventArgs e)
        {
            anp.RecordCount = dao.CalcCount(GetCond());
            BindRep();
        }

如果新建修改頁面:如href='link_mod.aspx?id=<%#Eval("id") %>'傳入id
string id = Request.QueryString["id"];
int i_id;
if (!string.IsNullOrEmpty(id) && int.TryParse(id, out i_id))
{
Model.Link model = dao.GetModel(i_id);
if (model != null)
{
model.linkurl = linkurl;
model.linkname = linkname;
}
dao.Update(model);
}
主站蜘蛛池模板: 漳平市| 马尔康县| 佛学| 通榆县| 屏东县| 崇义县| 息烽县| 敦化市| 榆树市| 应城市| 喀喇| 湄潭县| 建水县| 无棣县| 大洼县| 永年县| 长泰县| 双江| 新建县| 黔西县| 军事| 全南县| 龙岩市| 枝江市| 永平县| 两当县| 鹤山市| 陇川县| 克什克腾旗| 盱眙县| 嘉禾县| 横山县| 军事| 留坝县| 无锡市| 盱眙县| 乐都县| 长垣县| 都昌县| 佛教| 宁陵县|