最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

asp.net小孔子cms中的數(shù)據(jù)添加修改

 更新時(shí)間:2008年08月14日 20:02:49   作者:  
最近都在看小孔子cms的代碼,其添加與修改數(shù)據(jù)十分方便,做下筆記,代碼主要提取自小孔子cms,去掉了不用的函數(shù)并把相關(guān)代碼寫到一個(gè)文件中
題外話:我為什么研究小孔子的cms,從我自己寫一個(gè)cms我就開始研究起別人的cms,早期我是研究netcms,但這系統(tǒng)過于龐大,看上去十分的累,也沒那個(gè)精力,于是打算從一套比較小的開始研究,于是小孔子cms就進(jìn)入了我的研究范圍。沒過多久我就放下我手中的cms,決定研究清楚有了更多經(jīng)驗(yàn)再繼續(xù)寫完我沒有完成的cms。

       最近都在看小孔子cms的代碼,其添加與修改數(shù)據(jù)十分方便,做下筆記,代碼主要提取自小孔子cms,去掉了不用的函數(shù)并把相關(guān)代碼寫到一個(gè)文件中:

結(jié)合上面的圖片,當(dāng)我們要往數(shù)據(jù)庫中添加數(shù)據(jù)時(shí),代碼如下:

dataHandle doh = new dataHandle();    
doh.AddBind(tbxWebName, "link_webname", true);    
doh.AddBind(tbxWebUrl, "link_weburl", true);    
doh.AddBind(tbxLogoUrl, "link_logourl", true);    
doh.AddBind(tbxOrderNum, "link_ordernum", false);    
doh.AddBind(ddlStyle, "link_style", false);    
doh.AddBind(rblAudit, "link_audit", false);    
doh.Add();    
int result = Convert.ToInt32(doh.InsertData("db_link"));    
Response.Write(result.ToString());   
綁定數(shù)據(jù)指的是從數(shù)據(jù)庫中讀取一條記錄,并自動(dòng)綁定到表單的控件中,代碼如下(假設(shè)讀取的id=8):
復(fù)制代碼 代碼如下:


dataHandle doh = new dataHandle();     
doh.AddBind(tbxWebName, "link_webname", true);     
doh.AddBind(tbxWebUrl, "link_weburl", true);     
doh.AddBind(tbxLogoUrl, "link_logourl", true);     
doh.AddBind(tbxOrderNum, "link_ordernum", false);     
doh.AddBind(ddlStyle, "link_style", false);     
doh.AddBind(rblAudit, "link_audit", false);     
doh.ConditionExpress = "id = 8";     
doh.tableName = "db_link";     
doh.BindWhenUp();    

修改數(shù)據(jù)與添加數(shù)據(jù)差不多:  
復(fù)制代碼 代碼如下:

dataHandle doh = new dataHandle();     
doh.ConditionExpress = "id = 8";     
doh.AddBind(tbxWebName, "link_webname", true);     
doh.AddBind(tbxWebUrl, "link_weburl", true);     
doh.AddBind(tbxLogoUrl, "link_logourl", true);     
doh.AddBind(tbxOrderNum, "link_ordernum", false);     
doh.AddBind(ddlStyle, "link_style", false);     
doh.AddBind(rblAudit, "link_audit", false);     
doh.Add();     
int result = Convert.ToInt32(doh.UpData("db_link"));     
Response.Write(result);   

而aspx文件詳細(xì)代碼: XML/HTML復(fù)制代碼
網(wǎng)站:
<asp:TextBox ID="tbxWebName" runat="server"></asp:TextBox>   
<br />   
<br />   
域名:<asp:TextBox ID="tbxWebUrl" runat="server"></asp:TextBox><br />   
<br />   
logo地址:<asp:TextBox ID="tbxLogoUrl" runat="server" Width="198px"></asp:TextBox><br />   
<br />   
排序:<asp:TextBox ID="tbxOrderNum" runat="server"></asp:TextBox><br />   
<br />   
是否審核:<asp:RadioButtonList ID="rblAudit" runat="server" RepeatDirection="Horizontal">   
    <asp:ListItem Value="1">是</asp:ListItem>   
    <asp:ListItem Selected="True" Value="0">否</asp:ListItem>   
</asp:RadioButtonList>   
<br />   
<br />   
顯示方式:     
<br />   
<asp:DropDownList ID="ddlStyle" runat="server">   
    <asp:ListItem Value="1">文字</asp:ListItem>   
    <asp:ListItem Value="2">圖片</asp:ListItem>   
    <asp:ListItem Value="3">待定</asp:ListItem>   
</asp:DropDownList><br />   
<br />   
<asp:Button ID="btnOk" runat="server" Text="提交" OnClick="btnOk_Click" /> <asp:Button   
    ID="btnEnter" runat="server" OnClick="btnEnter_Click" Text="綁定" />   
<asp:Button ID="btnUp" runat="server" OnClick="btnUp_Click" Text="更改" /><br />   
<br />   
<asp:Label ID="lblResult" runat="server" Text="結(jié)果"></asp:Label></div>   
我對(duì)代碼做了很多注釋,大家有興趣可以看看: 
復(fù)制代碼 代碼如下:

using System;     
using System.Data;     
using System.Configuration;     
using System.Collections;     
using System.Web;     
using System.Web.Security;     
using System.Web.UI;     
using System.Web.UI.WebControls;     
using System.Web.UI.WebControls.WebParts;     
using System.Web.UI.HtmlControls;     
using System.Data.OleDb;     
using System.Text;     

namespace mycms.DataOper.Data     
{     
    /// <summary>     
    /// dataHandle 的摘要說明     
    /// </summary>     
    public class dataHandle     
    {     
        public dataHandle()     
        {     
            this.conn = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = |DataDirectory|mycms.mdb");     
            this.conn.Open();     
            this.cmd = conn.CreateCommand();     
            this.da = new OleDbDataAdapter();     
        }    

        #region webform     
        //這個(gè)用來存放包括控件類型,字段,是否是字符串         
        public ArrayList alBinderItems = new ArrayList(8);     

        //這個(gè)只用來存放字段,值         
        public ArrayList alFieldItems = new ArrayList(8);     

        /// <summary>         
        /// 建立文本框到數(shù)據(jù)字段的綁定         
        /// </summary>           
        public void AddBind(TextBox tbx, string field, bool isStringType)     
        {     
            alBinderItems.Add(new BinderItem(tbx, field, isStringType));     
        }     

        /// <summary>     
        /// 下拉列表     
        /// </summary>     
        public void AddBind(DropDownList dd, string field, bool isStringType)     
        {     
            alBinderItems.Add(new BinderItem(dd, field, isStringType));     
        }     

        public void AddBind(RadioButtonList rb, string field, bool isStringType)     
        {     
            alBinderItems.Add(new BinderItem(rb, field, isStringType));     
        }     

        /// <summary>     
        /// 多選框     
        /// </summary>     
        public void AddBind(CheckBoxList cb, string field, bool isStringType)     
        {     
            alBinderItems.Add(new BinderItem(cb, field, isStringType));     
        }     

        /// <summary>     
        /// 需要修改數(shù)據(jù)時(shí)取出數(shù)據(jù)庫中的記錄填充到表單中     
        /// </summary>     
        public void BindWhenUp()     
        {     
            if (alBinderItems.Count == 0)     
            {     
                return;     
            }     
            BinderItem bi;     

    
            StringBuilder sbSql = new StringBuilder("select ");     
            for (int i = 0; i < alBinderItems.Count; i++)     
            {     
                bi = (BinderItem)alBinderItems[i];     
                //防止出現(xiàn)變量名     
                sbSql.Append("[" + bi.field + "]");     
                sbSql.Append(",");     
            }     
            sbSql.Remove(sbSql.Length - 1,1);     
            sbSql.Append(" from ");     
            sbSql.Append(this.tableName);     
            sbSql.Append(" where 1 = 1 and ");     
            sbSql.Append(this.ConditionExpress);     

            this.sqlCmd = sbSql.ToString();     
            dt = this.GetDataTable();     
            //如果沒有記錄則拋出異常     
            if (dt.Rows.Count == 0)     
            {     
               throw new ArgumentException("記錄不存在");     
            }     

            DataRow dr = dt.Rows[0];     
            for (int j = 0; j < alBinderItems.Count; j++)     
            {     
                bi = (BinderItem)alBinderItems[j];     
                bi.SetValue(dr[bi.field].ToString());     
            }     

                 
        }     

        /// <summary>         
        /// 該方法實(shí)現(xiàn)從alBinderItems到alFieldItems的轉(zhuǎn)換,目的:alFieldItems可以轉(zhuǎn)為DbKeyItem,操作數(shù)據(jù)庫時(shí)需要用到DbKeyItem     
        /// </summary>         
        public void Add()     
        {     
            if (this.alBinderItems.Count == 0)     
            {     
                return;     
            }     
            BinderItem bi = null;     

            for (int i = 0; i < alBinderItems.Count; i++)     
            {     
                bi = ((BinderItem)alBinderItems[i]);     
                AddFieldItem(bi.field, bi.GetValue());     
            }     

        }     

        /// <summary>             
        /// 添加一個(gè)字段/值對(duì)到數(shù)組中             
        /// </summary>             
        public void AddFieldItem(string _fieldName, object _fieldValue)     
        {     
            _fieldName = "[" + _fieldName + "]";     
            //遍歷看是否已經(jīng)存在字段名             

            for (int i = 0; i < this.alFieldItems.Count; i++)     
            {     
                if (((DbKeyItem)this.alFieldItems[i]).fieldName == _fieldName)     
                {     
                    throw new ArgumentException("字段已經(jīng)存在");     
                }     
            }     
            this.alFieldItems.Add(new DbKeyItem(_fieldName, _fieldValue));     
        }    
        #endregion    

   
        #region 操作數(shù)據(jù)    

        #region 這里聲明有關(guān)數(shù)據(jù)操作的必要參數(shù)           
        //當(dāng)前所使用的數(shù)據(jù)庫連接     

        protected OleDbConnection conn;     

        //當(dāng)前所使用的命令對(duì)象     
        protected OleDbCommand cmd = new OleDbCommand();     

        //當(dāng)前所使用的數(shù)據(jù)庫適配器     
        protected OleDbDataAdapter da;     

        //當(dāng)前的SQL語句     
        public string sqlCmd = string.Empty;     

        //當(dāng)前操作所涉及的數(shù)據(jù)庫表名     
        public string tableName = string.Empty;     

        //SQL條件     
        public string ConditionExpress;     

        //用于存放從數(shù)據(jù)庫中取得的數(shù)據(jù)記錄     
        protected DataTable dt;    
        #endregion     

        /// <summary>     
        /// 根據(jù)當(dāng)前alFieldItem數(shù)組中存儲(chǔ)的字段/值向指定表中添加一條記錄。返回自動(dòng)增長(zhǎng)id     
        /// </summary>     
        /// <param name="_talbeName"></param>     
        /// <returns></returns>     
        public int InsertData(string _talbeName)     
        {     
            this.tableName = _talbeName;     
            this.sqlCmd = "insert into " + this.tableName + "(";     
            string temValue = " values(";     
            for (int i = 0; i < this.alFieldItems.Count; i++)     
            {     
                this.sqlCmd += ((DbKeyItem)alFieldItems[i]).fieldName + ",";     
                temValue += "@para" + i.ToString() + ",";     
            }     
            //分別去掉,         
            this.sqlCmd = Input.CutComma(this.sqlCmd) + ")" + Input.CutComma(temValue) + ")";     

            //聲明執(zhí)行語句     
            this.cmd.CommandText = this.sqlCmd;     
            GenParameters();     
            cmd.ExecuteNonQuery();     
            int autoId = 0;     
            try    
            {     
                cmd.CommandText = "select @@identity as id";     
                autoId = Convert.ToInt32(cmd.ExecuteScalar());     
            }     
            catch (Exception ex)     
            {     
                throw new Exception(ex.Message);     
            }     
            return autoId;     

        }     

        /// <summary>     
        /// 根據(jù)當(dāng)前alFieldItem數(shù)組中存儲(chǔ)的字段/值和條件表達(dá)式所指定的條件來更新數(shù)據(jù)庫中的記錄,返回受影響的行數(shù)     
        /// </summary>     
        /// <param name="_tableName">更新的數(shù)據(jù)表名稱</param>     
        /// <returns>返回此次操作所影響的數(shù)據(jù)行數(shù)</returns>     
        public int UpData(string _tableName)     
        {     
            this.tableName = _tableName;     
            this.sqlCmd = "update " + this.tableName + " set ";     
            for (int i = 0; i < this.alFieldItems.Count; i++)     
            {     
                this.sqlCmd += ((DbKeyItem)alFieldItems[i]).fieldName;     
                this.sqlCmd += "=";     
                this.sqlCmd += "@para";     
                this.sqlCmd += i.ToString();     
                this.sqlCmd += ",";     
            }     
            this.sqlCmd = Input.CutComma(this.sqlCmd);     
            if (this.ConditionExpress != string.Empty)     
            {     
                this.sqlCmd = this.sqlCmd + " where " + this.ConditionExpress;     
            }     
            this.cmd.CommandText = this.sqlCmd;     
            this.GenParameters();     
            int effectedLines = this.cmd.ExecuteNonQuery();     
            return effectedLines;     

        }     

        /// 返回查詢結(jié)果DataTable     
        public DataTable GetDataTable()     
        {     
            DataSet ds = this.GetDataSet();     
            return ds.Tables[0];     
        }     

        /// <summary>     
        /// 根據(jù)當(dāng)前指定的SqlCmd獲取DataSet,如果條件表達(dá)式不為空則會(huì)被清空,     
        /// 所以條件表達(dá)式必須包含在SqlCmd中     
        /// </summary>     
        public DataSet GetDataSet()     
        {     
            this.ConditionExpress = string.Empty;     
            this.cmd.CommandText = this.sqlCmd;     
            this.GenParameters();     
            DataSet ds = new DataSet();     
            this.da.SelectCommand = this.cmd;     
            this.da.Fill(ds);     
            return ds;     
        }     

    
        /// <summary>     
        /// 產(chǎn)生OleDbCommand對(duì)象所需的參數(shù)     
        /// </summary>     
        /// <returns></returns>     
        protected void GenParameters()     
        {     

            if (this.alFieldItems.Count > 0)     
            {     
                for (int i = 0; i < this.alFieldItems.Count; i++)     
                {     
                    cmd.Parameters.AddWithValue("@para" + i.ToString(), ((DbKeyItem)alFieldItems[i]).fieldValue.ToString());     
                }     
            }     
        }    
        #endregion     
    }     

    
    public class BinderItem     
    {     
        //每個(gè)綁定控件都以object的形式被存儲(chǔ)的         
        public object obj;     

        //綁定到數(shù)據(jù)庫的字段名稱         
        public string field;     

        //是否是字符串類型         
        public bool isStringType;     

        /// <summary>         
        /// 構(gòu)造函數(shù)         
        /// </summary>         
        /// <param name="_o">需要綁定的控件對(duì)象</param>         
        /// <param name="_field">綁定到的數(shù)據(jù)表字段名稱</param>         
        /// <param name="_isStringType">是否是字符串類型</param>         
        public BinderItem(object _obj, string _field, bool _isStringType)     
        {     
            this.obj = _obj;     
            this.field = _field;     
            this.isStringType = _isStringType;     
        }     

        /// <summary>         
        /// 根據(jù)控件類型獲得控件的值         
        /// </summary>         
        /// <returns></returns>         
        public string GetValue()     
        {     
            //字符串類型         
            if (obj is String)     
            {     
                return (string)obj;     
            }     

            //下拉框         
            if (obj is DropDownList)     
            {     
                DropDownList dd = (DropDownList)obj;     
                return dd.SelectedValue;     
            }     

            //多選框     
            if (obj is CheckBoxList)     
            {     
                string s = string.Empty;     
                CheckBoxList cb = (CheckBoxList)obj;     
                for (int i = 0; i < cb.Items.Count; i++)     
                {     
                    if (cb.Items[i].Selected)     
                    {     
                        s += cb.Items[i].Value + ",";     
                    }     
                }     
                return s;     
            }     

            //文本框         
            if (obj is TextBox)     
            {     
                TextBox tbx = (TextBox)obj;     
                return tbx.Text.Trim();     
            }     

            //Label         
            if (obj is Label)     
            {     
                Label lbl = (Label)obj;     
                return lbl.Text;     
            }     

            //單選組     
            if (obj is RadioButtonList)     
            {     
                RadioButtonList rb = (RadioButtonList)obj;     
                return rb.SelectedValue;     
            }     
            return string.Empty;     
        }     

        /// <summary>     
        /// 根據(jù)控件類型設(shè)定控件的值     
        /// </summary>     
        /// <param name="_value">要設(shè)定的值</param>     
        public void SetValue(string _value)     
        {     
            //字符串類型     
            if (obj is string)     
            {     
                string s = (string)obj;     
                s = _value;     
                return;     
            }     

            //文本框     
            if (obj is TextBox)     
            {     
                TextBox tbx = (TextBox)obj;     
                tbx.Text = _value;     
                    return;     
            }     

            //單選按鈕     
            if (obj is RadioButtonList)     
            {     
                RadioButtonList rb = (RadioButtonList)obj;     
                rb.SelectedValue = _value;     
                return;     
            }     

            //下拉列表     
            if (obj is DropDownList)     
            {     
                DropDownList dd = (DropDownList)obj;     
                dd.SelectedValue = _value;     
                return;     
            }     

        }     
    }     

    
    /// <summary>         
    /// 數(shù)據(jù)表中的字段屬性:字段名,字段值         
    /// </summary>         
    public class DbKeyItem     
    {     
        /// <summary>         
        /// 字段名稱         
        /// </summary>         
        public string fieldName;     

        /// <summary>         
        /// 字段值         
        /// </summary>         
        public string fieldValue;     

        public DbKeyItem(string _fileName, object _fieldValue)     
        {     
            this.fieldName = _fileName;     
            this.fieldValue = _fieldValue.ToString();     
        }     
    }     
}  
 return;     
            }     

            //單選按鈕     
            if (obj is RadioButtonList)     
            {     
                RadioButtonList rb = (RadioButtonList)obj;     
                rb.SelectedValue = _value;     
                return;     
            }     

            //下拉列表     
            if (obj is DropDownList)     
            {     
                DropDownList dd = (DropDownList)obj;     
                dd.SelectedValue = _value;     
                return;     
            }     

        }     
    }     

    
    /// <summary>         
    /// 數(shù)據(jù)表中的字段屬性:字段名,字段值         
    /// </summary>         
    public class DbKeyItem     
    {     
        /// <summary>         
        /// 字段名稱         
        /// </summary>         
        public string fieldName;     

        /// <summary>         
        /// 字段值         
        /// </summary>         
        public string fieldValue;     

        public DbKeyItem(string _fileName, object _fieldValue)     
        {     
            this.fieldName = _fileName;     
            this.fieldValue = _fieldValue.ToString();     
        }     
    }     
}  

相關(guān)文章

  • ASPNET中JSON的序列化和反序列化的方法

    ASPNET中JSON的序列化和反序列化的方法

    這篇文章主要介紹了ASPNET中JSON的序列化和反序列化的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2016-11-11
  • asp.net 使用Response.Filter 過濾非法詞匯

    asp.net 使用Response.Filter 過濾非法詞匯

    一般信息發(fā)布網(wǎng)站,論壇等均具有實(shí)現(xiàn)非法詞匯過濾功能,即當(dāng)用戶錄入非法詞匯時(shí),進(jìn)行替換,使其無法顯示到頁面上,針對(duì)此種功能,通常采用的時(shí),在讀取時(shí),在讀到非法詞匯后,進(jìn)行替換。
    2010-03-03
  • ASP.NET Core MVC/WebApi基礎(chǔ)系列1

    ASP.NET Core MVC/WebApi基礎(chǔ)系列1

    這篇文章主要介紹了ASP.NET Core MVC/WebApi基礎(chǔ)系列,后續(xù)會(huì)穿插講EF Core和ASP.NET Core,雖說是基礎(chǔ)系列但也是也有你不知道的。
    2019-04-04
  • ASP.NET中彈出消息框的幾種常見方法

    ASP.NET中彈出消息框的幾種常見方法

    這篇文章主要介紹了ASP.NET中彈出消息框的幾種常見方法,給出了幾種常見的彈出消息框的方法,并總結(jié)對(duì)比了幾種方法的特點(diǎn),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-11-11
  • ASP.NET?MVC5網(wǎng)站開發(fā)之用戶添加和瀏覽2(七)

    ASP.NET?MVC5網(wǎng)站開發(fā)之用戶添加和瀏覽2(七)

    這篇文章主要為大家詳細(xì)介紹了ASP.NET?MVC5網(wǎng)站開發(fā)之用戶添加和瀏覽,感興趣的小伙伴們可以參考一下
    2016-08-08
  • .NET性能優(yōu)化之為集合類型設(shè)置初始大小的方法

    .NET性能優(yōu)化之為集合類型設(shè)置初始大小的方法

    這篇文章主要介紹了.NET性能優(yōu)化之為集合類型設(shè)置初始大小的方法,今天要談的一個(gè)性能優(yōu)化的Tips是一個(gè)老生常談的點(diǎn),但是也是很多人沒有注意的一個(gè)點(diǎn)。在使用集合類型是,你應(yīng)該設(shè)置一個(gè)預(yù)估的初始大小,那么為什么需要這樣做?我們一起來從源碼的角度說一說
    2022-05-05
  • VS Code 1.26 發(fā)布 有你想要的新特性?

    VS Code 1.26 發(fā)布 有你想要的新特性?

    VS Code 1.26 發(fā)布,有你想要的新特性嗎?這篇文章主要為大家詳細(xì)介紹了VS Code 1.26一些主要的更新亮點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 在?.NET?MAUI?中加載?json?文件的方法

    在?.NET?MAUI?中加載?json?文件的方法

    這篇文章主要介紹了如何在?.NET?MAUI?中加載?json?文件,本文給大家介紹的非常詳細(xì)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-11-11
  • ASP.NET?Core?MVC中的標(biāo)簽助手(TagHelper)用法

    ASP.NET?Core?MVC中的標(biāo)簽助手(TagHelper)用法

    這篇文章介紹了ASP.NET?Core?MVC中標(biāo)簽助手(TagHelper)的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • ASP.NET中使用IFRAME建立類Modal窗口

    ASP.NET中使用IFRAME建立類Modal窗口

    ASP.NET中使用IFRAME建立類Modal窗口...
    2006-09-09

最新評(píng)論

垣曲县| 长丰县| 阜南县| 武鸣县| 拉萨市| 巨野县| 邹平县| 包头市| 绥阳县| 太保市| 泾阳县| 湖口县| 西青区| 桃园县| 沙河市| 五华县| 灵台县| 雅江县| 富裕县| 锦屏县| 黑河市| 竹溪县| 信丰县| 古蔺县| 奉节县| 芒康县| 梓潼县| 金乡县| 巴彦县| 灵宝市| 泰来县| 新竹县| 台北县| 河西区| 隆回县| 通州区| 盖州市| 南投市| 长丰县| 藁城市| 泰和县|