(原创)使用TimeStamp控制并发问题[示例]-.cs脚本

TimeStamp.aspx.cs代码(C#),已调试通过:
  1 None.gif using  System;
  2 None.gif using  System.Collections;
  3 None.gif using  System.ComponentModel;
  4 None.gif using  System.Data;
  5 None.gif using  System.Drawing;
  6 None.gif using  System.Web;
  7 None.gif using  System.Web.SessionState;
  8 None.gif using  System.Web.UI;
  9 None.gif using  System.Web.UI.WebControls;
 10 None.gif using  System.Web.UI.HtmlControls;
 11 None.gif
 12 None.gif using  System.Text;
 13 None.gif using  System.Data.SqlClient;
 14 None.gif using  DataAccess;
 15 None.gif
 16 None.gif namespace  TimeStamp
 17 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 18ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 19InBlock.gif    /// WebForm1 的摘要说明。
 20ExpandedSubBlockEnd.gif    /// </summary>

 21InBlock.gif    public class TimeStamp : System.Web.UI.Page
 22ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 23InBlock.gif        protected System.Web.UI.WebControls.TextBox tbKey;
 24InBlock.gif        protected System.Web.UI.WebControls.TextBox tbValue;
 25InBlock.gif        protected System.Web.UI.WebControls.TextBox tbTimeStamp;
 26InBlock.gif
 27InBlock.gif        private DataTable dt;
 28InBlock.gif        protected System.Web.UI.WebControls.Button Button1;
 29InBlock.gif        protected System.Web.UI.WebControls.Label Label1;
 30InBlock.gif    
 31InBlock.gif        protected string ConnectString = "server=oylb;User ID=sa;Password=;database=TimeStamp;Connection Reset=FALSE";
 32InBlock.gif
 33InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
 34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 35InBlock.gif            if (!IsPostBack)
 36ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 37InBlock.gif                BindData(1);
 38ExpandedSubBlockEnd.gif            }
    
 39ExpandedSubBlockEnd.gif        }

 40InBlock.gif
 41InBlock.gif        //取得数据
 42InBlock.gif        private void GetData(int kid)
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 44InBlock.gif            DataSet ds = new DataSet();
 45InBlock.gif
 46ExpandedSubBlockStart.gifContractedSubBlock.gif            String[] sArrayTableName = dot.gif{"TimeStamp"};
 47InBlock.gif            SqlHelper.FillDataset(ConnectString,CommandType.StoredProcedure,
 48InBlock.gif                                    "GetData",ds,sArrayTableName,
 49InBlock.gif                                        new SqlParameter("@KID",kid));
 50InBlock.gif
 51InBlock.gif            dt = ds.Tables["TimeStamp"];
 52InBlock.gif
 53InBlock.gif            //保存于Session,用于更新
 54InBlock.gif            Session["TimeStamp"= dt;
 55ExpandedSubBlockEnd.gif        }

 56InBlock.gif
 57InBlock.gif        //绑定数据到控件
 58InBlock.gif        private void BindData(int kid)
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 60InBlock.gif            GetData(kid);
 61InBlock.gif
 62InBlock.gif            Byte[] myByte;
 63InBlock.gif
 64InBlock.gif            this.tbKey.Text = dt.Rows[0]["KID"].ToString();
 65InBlock.gif            this.tbValue.Text = dt.Rows[0]["Name"].ToString();
 66InBlock.gif
 67InBlock.gif            myByte = (System.Byte[])dt.Rows[0]["TimeStamp"];
 68InBlock.gif
 69InBlock.gif            this.tbTimeStamp.Text = Encoding.ASCII.GetString(myByte,0,myByte.Length);
 70ExpandedSubBlockEnd.gif        }

 71InBlock.gif
 72InBlock.gif        //更新数据
 73InBlock.gif        private bool UpdateData()
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 75InBlock.gif            dt = (DataTable)Session["TimeStamp"];
 76InBlock.gif            
 77InBlock.gif            DataRow row = dt.Rows[0];
 78InBlock.gif
 79InBlock.gif            row["Name"= this.tbValue.Text.Trim();
 80InBlock.gif
 81InBlock.gif            dt.AcceptChanges();
 82InBlock.gif
 83InBlock.gif            int iEffect = SqlHelper.ExecuteNonQuery(ConnectString,"UpdateData",GetParam(row));
 84InBlock.gif
 85InBlock.gif            if (iEffect != 1)
 86ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 87InBlock.gif                return false;
 88ExpandedSubBlockEnd.gif            }

 89InBlock.gif
 90InBlock.gif            return true;
 91ExpandedSubBlockEnd.gif        }

 92InBlock.gif
 93InBlock.gif        //根据datarow获取参数
 94InBlock.gif        private SqlParameter[] GetParam(DataRow row)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 96InBlock.gif            DataColumnCollection cols;
 97InBlock.gif
 98InBlock.gif            cols = row.Table.Columns;
 99InBlock.gif
100InBlock.gif            int iBound = cols.Count;
101InBlock.gif                
102InBlock.gif            SqlParameter[] Params = new SqlParameter[iBound];
103InBlock.gif            SqlParameter Param = new SqlParameter();
104InBlock.gif
105InBlock.gif            string strColumnName;
106InBlock.gif            int iIndex = 0;
107InBlock.gif
108InBlock.gif            object oRowValue = null;
109InBlock.gif
110InBlock.gif            foreach(DataColumn col in cols)
111ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
112InBlock.gif                strColumnName = col.ColumnName;
113InBlock.gif
114InBlock.gif                oRowValue = row[col];
115InBlock.gif
116InBlock.gif                if (oRowValue == System.DBNull.Value)
117ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
118InBlock.gif                    oRowValue = DBNull.Value;
119ExpandedSubBlockEnd.gif                }

120InBlock.gif
121InBlock.gif                if (col.DataType.ToString() == "System.Byte[]")
122ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
123InBlock.gif                    //专门处理Timestamp
124InBlock.gif                    Param = new SqlParameter("@"+strColumnName,SqlDbType.Timestamp, 8);
125InBlock.gif                    Param.Direction = ParameterDirection.Input;
126InBlock.gif                    Param.Value = oRowValue; 
127ExpandedSubBlockEnd.gif                }

128InBlock.gif                else
129ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
130InBlock.gif                    Param = new SqlParameter();
131InBlock.gif
132InBlock.gif                    Param.ParameterName = "@"+strColumnName;
133InBlock.gif                    Param.SqlDbType = GetColSqlDbType(col);
134InBlock.gif                    Param.Direction = ParameterDirection.Input;
135InBlock.gif                    Param.Value = oRowValue;
136ExpandedSubBlockEnd.gif                }

137InBlock.gif                
138InBlock.gif                Params[iIndex] = Param;
139InBlock.gif                iIndex ++;
140ExpandedSubBlockEnd.gif            }

141InBlock.gif
142InBlock.gif            return Params;
143ExpandedSubBlockEnd.gif        }

144InBlock.gif
145InBlock.gif        //转化类型
146InBlock.gif        private SqlDbType GetColSqlDbType(DataColumn col)
147ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
148InBlock.gif            string strType = col.DataType.ToString();
149InBlock.gif
150InBlock.gif            switch(strType)
151ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
152InBlock.gif                case "System.Int32":
153InBlock.gif                    return SqlDbType.Int;
154InBlock.gif                case "System.DateTime":
155InBlock.gif                    return SqlDbType.DateTime;
156InBlock.gif                case "System.Double":
157InBlock.gif                    return SqlDbType.Decimal;
158InBlock.gif                case "System.String":
159InBlock.gif                    return SqlDbType.Char;
160InBlock.gif                case "System.Byte[]":
161InBlock.gif                    return SqlDbType.Timestamp;
162InBlock.gif                default:
163InBlock.gif                    return SqlDbType.Char;
164ExpandedSubBlockEnd.gif            }

165ExpandedSubBlockEnd.gif        }

166ExpandedSubBlockStart.gifContractedSubBlock.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
167InBlock.gif        override protected void OnInit(EventArgs e)
168ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
169InBlock.gif            //
170InBlock.gif            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
171InBlock.gif            //
172InBlock.gif            InitializeComponent();
173InBlock.gif            base.OnInit(e);
174ExpandedSubBlockEnd.gif        }

175InBlock.gif        
176ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
177InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
178InBlock.gif        /// 此方法的内容。
179ExpandedSubBlockEnd.gif        /// </summary>

180InBlock.gif        private void InitializeComponent()
181ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
182InBlock.gif            this.Button1.Click += new System.EventHandler(this.Button1_Click);
183InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
184InBlock.gif
185ExpandedSubBlockEnd.gif        }

186ExpandedSubBlockEnd.gif        #endregion

187InBlock.gif
188InBlock.gif        private void Button1_Click(object sender, System.EventArgs e)
189ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
190InBlock.gif            if (UpdateData())
191ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
192InBlock.gif                this.Label1.Text = "成功!";
193InBlock.gif                BindData(1);
194ExpandedSubBlockEnd.gif            }

195InBlock.gif            else
196ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
197InBlock.gif                this.Label1.Text = "失败!";
198ExpandedSubBlockEnd.gif            }

199ExpandedSubBlockEnd.gif        }

200ExpandedSubBlockEnd.gif    }

201ExpandedBlockEnd.gif}

202 None.gif

(原创)使用TimeStamp控制并发问题[示例]-简要描述
(原创)使用TimeStamp控制并发问题[示例]-页面HTML脚本
(原创)使用TimeStamp控制并发问题[示例]-创建后台数据库脚本

转载于:https://www.cnblogs.com/Hedonister/archive/2005/06/10/172050.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值