1
using
System;
2 using System.Collections;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Web;
7 using System.Web.SessionState;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.HtmlControls;
11
12 using System.Text;
13 using System.Data.SqlClient;
14 using DataAccess;
15
16 namespace TimeStamp
17 {
18 /**//// <summary>
19 /// WebForm1 的摘要说明。
20 /// </summary>
21 public class TimeStamp : System.Web.UI.Page
22 {
23 protected System.Web.UI.WebControls.TextBox tbKey;
24 protected System.Web.UI.WebControls.TextBox tbValue;
25 protected System.Web.UI.WebControls.TextBox tbTimeStamp;
26
27 private DataTable dt;
28 protected System.Web.UI.WebControls.Button Button1;
29 protected System.Web.UI.WebControls.Label Label1;
30
31 protected string ConnectString = "server=oylb;User ID=sa;Password=;database=TimeStamp;Connection Reset=FALSE";
32
33 private void Page_Load(object sender, System.EventArgs e)
34 {
35 if (!IsPostBack)
36 {
37 BindData(1);
38 }
39 }
40
41 //取得数据
42 private void GetData(int kid)
43 {
44 DataSet ds = new DataSet();
45
46 String[] sArrayTableName = {"TimeStamp"};
47 SqlHelper.FillDataset(ConnectString,CommandType.StoredProcedure,
48 "GetData",ds,sArrayTableName,
49 new SqlParameter("@KID",kid));
50
51 dt = ds.Tables["TimeStamp"];
52
53 //保存于Session,用于更新
54 Session["TimeStamp"] = dt;
55 }
56
57 //绑定数据到控件
58 private void BindData(int kid)
59 {
60 GetData(kid);
61
62 Byte[] myByte;
63
64 this.tbKey.Text = dt.Rows[0]["KID"].ToString();
65 this.tbValue.Text = dt.Rows[0]["Name"].ToString();
66
67 myByte = (System.Byte[])dt.Rows[0]["TimeStamp"];
68
69 this.tbTimeStamp.Text = Encoding.ASCII.GetString(myByte,0,myByte.Length);
70 }
71
72 //更新数据
73 private bool UpdateData()
74 {
75 dt = (DataTable)Session["TimeStamp"];
76
77 DataRow row = dt.Rows[0];
78
79 row["Name"] = this.tbValue.Text.Trim();
80
81 dt.AcceptChanges();
82
83 int iEffect = SqlHelper.ExecuteNonQuery(ConnectString,"UpdateData",GetParam(row));
84
85 if (iEffect != 1)
86 {
87 return false;
88 }
89
90 return true;
91 }
92
93 //根据datarow获取参数
94 private SqlParameter[] GetParam(DataRow row)
95 {
96 DataColumnCollection cols;
97
98 cols = row.Table.Columns;
99
100 int iBound = cols.Count;
101
102 SqlParameter[] Params = new SqlParameter[iBound];
103 SqlParameter Param = new SqlParameter();
104
105 string strColumnName;
106 int iIndex = 0;
107
108 object oRowValue = null;
109
110 foreach(DataColumn col in cols)
111 {
112 strColumnName = col.ColumnName;
113
114 oRowValue = row[col];
115
116 if (oRowValue == System.DBNull.Value)
117 {
118 oRowValue = DBNull.Value;
119 }
120
121 if (col.DataType.ToString() == "System.Byte[]")
122 {
123 //专门处理Timestamp
124 Param = new SqlParameter("@"+strColumnName,SqlDbType.Timestamp, 8);
125 Param.Direction = ParameterDirection.Input;
126 Param.Value = oRowValue;
127 }
128 else
129 {
130 Param = new SqlParameter();
131
132 Param.ParameterName = "@"+strColumnName;
133 Param.SqlDbType = GetColSqlDbType(col);
134 Param.Direction = ParameterDirection.Input;
135 Param.Value = oRowValue;
136 }
137
138 Params[iIndex] = Param;
139 iIndex ++;
140 }
141
142 return Params;
143 }
144
145 //转化类型
146 private SqlDbType GetColSqlDbType(DataColumn col)
147 {
148 string strType = col.DataType.ToString();
149
150 switch(strType)
151 {
152 case "System.Int32":
153 return SqlDbType.Int;
154 case "System.DateTime":
155 return SqlDbType.DateTime;
156 case "System.Double":
157 return SqlDbType.Decimal;
158 case "System.String":
159 return SqlDbType.Char;
160 case "System.Byte[]":
161 return SqlDbType.Timestamp;
162 default:
163 return SqlDbType.Char;
164 }
165 }
166 Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
167 override protected void OnInit(EventArgs e)
168 {
169 //
170 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
171 //
172 InitializeComponent();
173 base.OnInit(e);
174 }
175
176 /**//// <summary>
177 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
178 /// 此方法的内容。
179 /// </summary>
180 private void InitializeComponent()
181 {
182 this.Button1.Click += new System.EventHandler(this.Button1_Click);
183 this.Load += new System.EventHandler(this.Page_Load);
184
185 }
186 #endregion
187
188 private void Button1_Click(object sender, System.EventArgs e)
189 {
190 if (UpdateData())
191 {
192 this.Label1.Text = "成功!";
193 BindData(1);
194 }
195 else
196 {
197 this.Label1.Text = "失败!";
198 }
199 }
200 }
201}
202
2 using System.Collections;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Web;
7 using System.Web.SessionState;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.HtmlControls;
11
12 using System.Text;
13 using System.Data.SqlClient;
14 using DataAccess;
15
16 namespace TimeStamp
17 {
18 /**//// <summary>
19 /// WebForm1 的摘要说明。
20 /// </summary>
21 public class TimeStamp : System.Web.UI.Page
22 {
23 protected System.Web.UI.WebControls.TextBox tbKey;
24 protected System.Web.UI.WebControls.TextBox tbValue;
25 protected System.Web.UI.WebControls.TextBox tbTimeStamp;
26
27 private DataTable dt;
28 protected System.Web.UI.WebControls.Button Button1;
29 protected System.Web.UI.WebControls.Label Label1;
30
31 protected string ConnectString = "server=oylb;User ID=sa;Password=;database=TimeStamp;Connection Reset=FALSE";
32
33 private void Page_Load(object sender, System.EventArgs e)
34 {
35 if (!IsPostBack)
36 {
37 BindData(1);
38 }
39 }
40
41 //取得数据
42 private void GetData(int kid)
43 {
44 DataSet ds = new DataSet();
45
46 String[] sArrayTableName = {"TimeStamp"};
47 SqlHelper.FillDataset(ConnectString,CommandType.StoredProcedure,
48 "GetData",ds,sArrayTableName,
49 new SqlParameter("@KID",kid));
50
51 dt = ds.Tables["TimeStamp"];
52
53 //保存于Session,用于更新
54 Session["TimeStamp"] = dt;
55 }
56
57 //绑定数据到控件
58 private void BindData(int kid)
59 {
60 GetData(kid);
61
62 Byte[] myByte;
63
64 this.tbKey.Text = dt.Rows[0]["KID"].ToString();
65 this.tbValue.Text = dt.Rows[0]["Name"].ToString();
66
67 myByte = (System.Byte[])dt.Rows[0]["TimeStamp"];
68
69 this.tbTimeStamp.Text = Encoding.ASCII.GetString(myByte,0,myByte.Length);
70 }
71
72 //更新数据
73 private bool UpdateData()
74 {
75 dt = (DataTable)Session["TimeStamp"];
76
77 DataRow row = dt.Rows[0];
78
79 row["Name"] = this.tbValue.Text.Trim();
80
81 dt.AcceptChanges();
82
83 int iEffect = SqlHelper.ExecuteNonQuery(ConnectString,"UpdateData",GetParam(row));
84
85 if (iEffect != 1)
86 {
87 return false;
88 }
89
90 return true;
91 }
92
93 //根据datarow获取参数
94 private SqlParameter[] GetParam(DataRow row)
95 {
96 DataColumnCollection cols;
97
98 cols = row.Table.Columns;
99
100 int iBound = cols.Count;
101
102 SqlParameter[] Params = new SqlParameter[iBound];
103 SqlParameter Param = new SqlParameter();
104
105 string strColumnName;
106 int iIndex = 0;
107
108 object oRowValue = null;
109
110 foreach(DataColumn col in cols)
111 {
112 strColumnName = col.ColumnName;
113
114 oRowValue = row[col];
115
116 if (oRowValue == System.DBNull.Value)
117 {
118 oRowValue = DBNull.Value;
119 }
120
121 if (col.DataType.ToString() == "System.Byte[]")
122 {
123 //专门处理Timestamp
124 Param = new SqlParameter("@"+strColumnName,SqlDbType.Timestamp, 8);
125 Param.Direction = ParameterDirection.Input;
126 Param.Value = oRowValue;
127 }
128 else
129 {
130 Param = new SqlParameter();
131
132 Param.ParameterName = "@"+strColumnName;
133 Param.SqlDbType = GetColSqlDbType(col);
134 Param.Direction = ParameterDirection.Input;
135 Param.Value = oRowValue;
136 }
137
138 Params[iIndex] = Param;
139 iIndex ++;
140 }
141
142 return Params;
143 }
144
145 //转化类型
146 private SqlDbType GetColSqlDbType(DataColumn col)
147 {
148 string strType = col.DataType.ToString();
149
150 switch(strType)
151 {
152 case "System.Int32":
153 return SqlDbType.Int;
154 case "System.DateTime":
155 return SqlDbType.DateTime;
156 case "System.Double":
157 return SqlDbType.Decimal;
158 case "System.String":
159 return SqlDbType.Char;
160 case "System.Byte[]":
161 return SqlDbType.Timestamp;
162 default:
163 return SqlDbType.Char;
164 }
165 }
166 Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
167 override protected void OnInit(EventArgs e)
168 {
169 //
170 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
171 //
172 InitializeComponent();
173 base.OnInit(e);
174 }
175
176 /**//// <summary>
177 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
178 /// 此方法的内容。
179 /// </summary>
180 private void InitializeComponent()
181 {
182 this.Button1.Click += new System.EventHandler(this.Button1_Click);
183 this.Load += new System.EventHandler(this.Page_Load);
184
185 }
186 #endregion
187
188 private void Button1_Click(object sender, System.EventArgs e)
189 {
190 if (UpdateData())
191 {
192 this.Label1.Text = "成功!";
193 BindData(1);
194 }
195 else
196 {
197 this.Label1.Text = "失败!";
198 }
199 }
200 }
201}
202
(原创)使用TimeStamp控制并发问题[示例]-简要描述
(原创)使用TimeStamp控制并发问题[示例]-页面HTML脚本
(原创)使用TimeStamp控制并发问题[示例]-创建后台数据库脚本