標籤:sum 實體 添加 open data count int name load
Application - 全域公開變數組
存放位置:服務端
所有的訪問使用者都是訪問的同一個變數
聲明周期:永久
用法同session類似
viewstate-病例
因為http的無狀態性,需要記錄上一個頁面的狀態,因而需要一個viewstate記錄上一個頁面的狀態,然後返回成相應的內容
純HTML提交:
HTML介面:
要寫action與method:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title></head><body> <form action="Default.aspx" method="get"> 貨品名稱:<input type="text" name="t1"/><br /> 貨品數量:<input type="text" name="t2"/><br /> 單價:        <input type="text" name="t3"/><br /> 進貨時間:<input type="text" name="t4"/><br /> 聯絡電話:<input type="text" name="t5"/><br /> 倉庫號:     <select id="Select1" name="t6"> <option>g01</option> <option>g02</option> <option>g03</option> </select> <br /> <input type="submit" value="提交" /> </form></body></html>
實體類:
using System;using System.Collections.Generic;using System.Linq;using System.Web;/// <summary>/// goods 的摘要說明/// </summary>public class goods{ public goods() { // // TODO: 在此處添加建構函式邏輯 // } public string goodsname { get; set; } public string number { get; set; } public string sprice { get; set; } public DateTime intime { get; set; } public string gtel { get; set; } public string goodsbase { get; set; }}
資料訪問類:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data.SqlClient;/// <summary>/// goodsdata 的摘要說明/// </summary>public class goodsdata{ SqlConnection conn = null; SqlCommand cmd = null; public goodsdata() { conn = new SqlConnection("server=.;database=data0928;user=sa;pwd=123"); cmd = conn.CreateCommand(); } public bool insert(goods g) { bool ok = false; int count = 0; cmd.CommandText = "insert into goods values(@a,@b,@c,@d,@e,@f)"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@a",g.goodsname); cmd.Parameters.AddWithValue("@b",g.number); cmd.Parameters.AddWithValue("@c",g.sprice); cmd.Parameters.AddWithValue("@d",DateTime.Now); cmd.Parameters.AddWithValue("@e",g.gtel); cmd.Parameters.AddWithValue("@f",g.goodsbase); conn.Open(); count = cmd.ExecuteNonQuery(); conn.Close(); if (count > 0) { ok = true; } return ok; }}
aspx介面:
using System;using System.Collections.Generic;using System.Data.SqlClient;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { goods g = new goods(); g.goodsname = Request["t1"]; g.number = Request["t2"]; g.sprice = Request["t3"]; g.intime = Convert.ToDateTime(Request["t4"]); this.Title = g.intime.ToString(); g.gtel = Request["t5"]; g.goodsbase = Request["t6"]; bool ok = new goodsdata().insert(g); if (ok) { Response.Write("<script>alert(‘添加成功‘)</script>"); } }}
application、viewstate、純HTML提交方式