天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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

題外話(huà):我為什么研究小孔子的cms,從我自己寫(xiě)一個(gè)cms我就開(kāi)始研究起別人的cms,早期我是研究NETcms,但這系統(tǒng)過(guò)于龐大,看上去十分的累,也沒(méi)那個(gè)精力,于是打算從一套比較小的開(kāi)始研究,于是小孔子cms就進(jìn)入了我的研究范圍。沒(méi)過(guò)多久我就放下我手中的cms,決定研究清楚有了更多經(jīng)驗(yàn)再繼續(xù)寫(xiě)完我沒(méi)有完成的cms。

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

結(jié)合上面的圖片,當(dāng)我們要往數(shù)據(jù)庫(kù)中添加數(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ù)庫(kù)中讀取一條記錄,并自動(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ì)代碼做了很多注釋?zhuān)蠹矣信d趣可以看看: 
復(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 的摘要說(shuō)明     
    /// </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è)用來(lái)存放包括控件類(lèi)型,字段,是否是字符串         
        public ArrayList alBinderItems = new ArrayList(8);     

        //這個(gè)只用來(lái)存放字段,值         
        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ù)庫(kù)中的記錄填充到表單中     
        /// </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();     
            //如果沒(méi)有記錄則拋出異常     
            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ù)庫(kù)時(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ù)庫(kù)連接     

        protected OleDbConnection conn;     

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

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

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

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

        //SQL條件     
        public string ConditionExpress;     

        //用于存放從數(shù)據(jù)庫(kù)中取得的數(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í)行語(yǔ)句     
            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á)式所指定的條件來(lái)更新數(shù)據(jù)庫(kù)中的記錄,返回受影響的行數(shù)     
        /// </summary>     
        /// <param name="_tableName">更新的數(shù)據(jù)表名稱(chēng)</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;     

        }     

        /// 返回查詢(xún)結(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ù)庫(kù)的字段名稱(chēng)         
        public string field;     

        //是否是字符串類(lèi)型         
        public bool isStringType;     

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

        /// <summary>         
        /// 根據(jù)控件類(lèi)型獲得控件的值         
        /// </summary>         
        /// <returns></returns>         
        public string GetValue()     
        {     
            //字符串類(lèi)型         
            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ù)控件類(lèi)型設(shè)定控件的值     
        /// </summary>     
        /// <param name="_value">要設(shè)定的值</param>     
        public void SetValue(string _value)     
        {     
            //字符串類(lèi)型     
            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>         
        /// 字段名稱(chēng)         
        /// </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>         
        /// 字段名稱(chēng)         
        /// </summary>         
        public string fieldName;     

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

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

AspNet技術(shù)asp.net小孔子cms中的數(shù)據(jù)添加修改,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产精品久久久久久影院 | 免费视频久久只有精品 | 美女尿口羞羞视频 | 美女洗澡脱得一二干净 | 国产女人喷潮视频免费 | 国产一区二区在线观看免费 | 受被攻做到腿发颤高h文 | 亚洲.日韩.欧美另类 | 久久亚洲免费视频 | 亚洲AV久久久噜噜噜噜 | 亚洲黄色在线 | 久久综合狠狠综合久久综合88 | 大岛优香久久中文字幕 | 熟女人妻AV五十路六十路 | 乱h好大噗嗤噗嗤烂了 | 青青青久草 | 多肉np一女多男高h爽文现代 | 龙腾亚洲人成电影网站 | 成电影人免费网站 | 99视频在线免费观看 | 亚洲人成伊人成综合网久久久 | 成人在线视频免费观看 | 国产a级黄色毛片 | 色琪琪无码成人AV视频 | 日日噜噜夜夜狠狠视频 | 久久精品影院永久网址 | 高清视频在线观看SEYEYE | 日韩精品AV一区二区三区 | 97精品在线观看 | 亚洲精品日韩在线观看视频 | 吃奶摸下的羞羞漫画 | 在线高清电影理论片4399 | 亚洲 欧美 国产在线视频 | 色午夜日本高清视频www | 亚洲精品123区在线观看 | 91亚洲 欧美 国产 制服 动漫 | 97视频国产| 久久热最新网站获取3 | brazzers巨臀系列| 美女PK精子小游戏 | 人妻系列合集 |