• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

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

    來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 22:45:01
    文檔

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

    asp.net小孔子cms中的數(shù)據(jù)添加修改:題外話:我為什么研究小孔子的cms,從我自己寫一個cms我就開始研究起別人的cms,早期我是研究netcms,但這系統(tǒng)過于龐大,看上去十分的累,也沒那個精力,于是打算從一套比較小的開始研究,于是小孔子cms就進入了我的研究范圍。沒過多久我就放下我手中的cms
    推薦度:
    導(dǎo)讀asp.net小孔子cms中的數(shù)據(jù)添加修改:題外話:我為什么研究小孔子的cms,從我自己寫一個cms我就開始研究起別人的cms,早期我是研究netcms,但這系統(tǒng)過于龐大,看上去十分的累,也沒那個精力,于是打算從一套比較小的開始研究,于是小孔子cms就進入了我的研究范圍。沒過多久我就放下我手中的cms

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

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

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

    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ù)庫中讀取一條記錄,并自動綁定到表單的控件中,代碼如下(假設(shè)讀取的id=8):
    代碼如下:

    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ù)差不多:  
    代碼如下:
    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文件詳細代碼: XML/HTML
    網(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>   
    我對代碼做了很多注釋,大家有興趣可以看看: 
    代碼如下:
    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     
            //這個用來存放包括控件類型,字段,是否是字符串         
            public ArrayList alBinderItems = new ArrayList(8);     

            //這個只用來存放字段,值         
            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ù)據(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>         
            /// 該方法實現(xiàn)從alBinderItems到alFieldItems的轉(zhuǎn)換,目的:alFieldItems可以轉(zhuǎn)為DbKeyItem,操作數(shù)據(jù)庫時需要用到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>             
            /// 添加一個字段/值對到數(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)前所使用的命令對象     
            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ù)組中存儲的字段/值向指定表中添加一條記錄。返回自動增長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ù)組中存儲的字段/值和條件表達式所指定的條件來更新數(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,如果條件表達式不為空則會被清空,     
            /// 所以條件表達式必須包含在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對象所需的參數(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     
        {     
            //每個綁定控件都以object的形式被存儲的         
            public object obj;     

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

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

            /// <summary>         
            /// 構(gòu)造函數(shù)         
            /// </summary>         
            /// <param name="_o">需要綁定的控件對象</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();     
            }     
        }     
    }  

    聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

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

    asp.net小孔子cms中的數(shù)據(jù)添加修改:題外話:我為什么研究小孔子的cms,從我自己寫一個cms我就開始研究起別人的cms,早期我是研究netcms,但這系統(tǒng)過于龐大,看上去十分的累,也沒那個精力,于是打算從一套比較小的開始研究,于是小孔子cms就進入了我的研究范圍。沒過多久我就放下我手中的cms
    推薦度:
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: AAA级久久久精品无码片| 久久久久99精品成人片直播| 宅男宅女精品国产AV天堂| 精品国产免费一区二区三区香蕉| 精品无人区无码乱码毛片国产 | 在线亚洲精品自拍| 国产精品免费视频观看拍拍 | 欧美成人精品高清视频在线观看 | 精品无码人妻一区二区三区不卡| 久久精品国产99国产精品澳门| 亚洲AV无码成人精品区在线观看 | 亚洲国产精品自产在线播放| 国产精品久操视频| 久久久久国产精品| 国产精品爱啪在线线免费观看| 全球AV集中精品导航福利| 亚洲精品成a人在线观看| 精品欧美一区二区在线看片| 一区二区三区国产精品| 国产亚洲综合成人91精品| 国产国产精品人在线视| 久久ww精品w免费人成| 亚洲av无码乱码国产精品fc2| 亚洲国产婷婷综合在线精品| 久久久久人妻一区精品| 无码日韩精品一区二区免费暖暖| 亚洲精品国产自在久久| 99久久精品久久久久久清纯| 日韩精品在线一区二区| 国产精品天天影视久久综合网 | 国产精品JIZZ在线观看老狼| 久久91综合国产91久久精品| 99re这里只有精品国产精品| 国产成人精品日本亚洲网站| 精品国产福利一区二区| 国产精品丝袜一区二区三区| 囯产精品一品二区三区| 国产精品一国产精品| 日本精品久久久中文字幕| 2021国产成人精品久久| 99久久免费只有精品国产|