大厨师

全局变量 的全局有多大??

有一个问题困扰我很长时间了,总是绞尽脑汁的想,也想不出所以然(当然主要是人笨)

default.aspx

<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="test._default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>default</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <asp:DropDownList id="DropDownList1" style="Z-INDEX: 101; LEFT: 232px; POSITION: absolute; TOP: 88px"
     runat="server" AutoPostBack="True"></asp:DropDownList>
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 232px; POSITION: absolute; TOP: 136px"
     runat="server"></asp:DataGrid>
    <asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 600px; POSITION: absolute; TOP: 88px" runat="server"
     Text="Button"></asp:Button>
    <asp:Label id="Label1" style="Z-INDEX: 104; LEFT: 232px; POSITION: absolute; TOP: 32px" runat="server"
     Width="161px" Height="24px">河北省</asp:Label></FONT></form>
  </FORM>
 </body>
</HTML>

default.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace test
{
 /// <summary>
 /// _default 的摘要说明。
 /// </summary>
 public class _default : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DropDownList DropDownList1;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
  protected System.Data.SqlClient.SqlCommand sqlSelectCommand2;
  protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
  protected System.Data.SqlClient.SqlConnection sqlConnection1;
  protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
  protected System.Web.UI.WebControls.Button Button1;
  static string somestring;
  
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   
   if(!IsPostBack)
   {      this.sqlSelectCommand1.Connection.Open();
   
    DropDownList1.DataSource=this.sqlSelectCommand1.ExecuteReader();
    DropDownList1.DataTextField="名称";
    DropDownList1.DataValueField="代码";
    DropDownList1.DataBind();
    this.sqlSelectCommand1.Connection.Close();
   }


  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
   this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
   this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand();
   this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
   this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
   this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   //
   // sqlSelectCommand1
   //
   this.sqlSelectCommand1.CommandText = "SELECT 代码, 名称 FROM 行政区划 where left(代码,2)=\'13\' and right(代码,2)=\'00\'";
   this.sqlSelectCommand1.Connection = this.sqlConnection1;
   //
   //
   // sqlSelectCommand2
   // 
   this.sqlSelectCommand2.CommandText = "SELECT 代码, 名称 FROM 行政区划 where left(代码,4)='"+somesting+"' and right(代码,2)=\'00\'";
   this.sqlSelectCommand2.Connection = this.sqlConnection1;// sqlConnection1
   //
   this.sqlConnection1.ConnectionString = "workstation id=\"THTFPC-ECEAB98C\";packet size=4096;integrated security=SSPI;data s" +
    "ource=\"THTFPC-ECEAB98C\";persist security info=True;initial catalog=Northwind";
   //
   // sqlInsertCommand1
   //
   this.sqlInsertCommand1.CommandText = "INSERT INTO 行政区划(代码, 名称) VALUES (@代码, @名称); SELECT 代码, 名称 FROM 行政区划";
   this.sqlInsertCommand1.Connection = this.sqlConnection1;
   this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@代码", System.Data.SqlDbType.VarChar, 6, "代码"));
   this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@名称", System.Data.SqlDbType.NVarChar, 15, "名称"));
   //
   // sqlDataAdapter1
   //
   this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
   this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
   this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
                           new System.Data.Common.DataTableMapping("Table", "行政区划", new System.Data.Common.DataColumnMapping[] {
                                                     new System.Data.Common.DataColumnMapping("代码", "代码"),
                                                     new System.Data.Common.DataColumnMapping("名称", "名称")})});
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   somestring=DropDownList1.SelectedValue.ToString();
   Label1.Text=somestring;
  }

  private void Button1_Click(object sender, System.EventArgs e)
  {
   string tmp=somestring;
   
   this.sqlSelectCommand2.CommandText = "SELECT 代码, 名称 FROM 行政区划 where left(代码,4)='"+tmp.Substring(0,4)+"'";
   this.sqlSelectCommand2.Connection.Open();
   
   DataGrid1.DataSource=this.sqlSelectCommand2.ExecuteReader();
      
   DataGrid1.DataBind();
   this.sqlSelectCommand2.Connection.Close();
  
  }

  
 }
}

 

数据
do{
130000    河北省
130100    石家庄市
130102    长安区
130103    桥东区
130104    桥西区
130105    新华区
130107    井陉矿区
130108    裕华区
130121    井陉县
130123    正定县
130124    栾城县
130125    行唐县
130126    灵寿县
130127    高邑县
130128    深泽县
130129    赞皇县
130130    无极县
130131    平山县
130132    元氏县
130133    赵县
130181    辛集市
130182    藁城市
130183    晋州市
130184    新乐市
130185    鹿泉市
130200    唐山市
130202    路南区
130203    路北区
130204    古冶区
130205    开平区
130207    丰南区
130208    丰润区
130223    滦县
130224    滦南县
130225    乐亭县
130227    迁西县
130229    玉田县
130230    唐海县
130281    遵化市
130283    迁安市
130300    秦皇岛市
130302    海港区
130303    山海关区
130304    北戴河区
130321    青龙满族自治县
130322    昌黎县
130323    抚宁县
130324    卢龙县
130400    邯郸市
130402    邯山区
130403    丛台区
130404    复兴区
130406    峰峰矿区
130421    邯郸县
130423    临漳县
130424    成安县
130425    大名县
130426    涉县
130427    磁县
130428    肥乡县
130429    永年县
130430    邱县
130431    鸡泽县
130432    广平县
130433    馆陶县
130434    魏县
130435    曲周县
130481    武安市
130500    邢台市
130502    桥东区
130503    桥西区
130521    邢台县
130522    临城县
130523    内丘县
130524    柏乡县
130525    隆尧县
130526    任县
130527    南和县
130528    宁晋县
130529    巨鹿县
130530    新河县
130531    广宗县
130532    平乡县
130533    威县
130534    清河县
130535    临西县
130581    南宫市
130582    沙河市
130600    保定市
130602    新市区
130603    北市区
130604    南市区
130621    满城县
130622    清苑县
130623    涞水县
130624    阜平县
130625    徐水县
130626    定兴县
130627    唐县
130628    高阳县
130629    容城县
130630    涞源县
130631    望都县
130632    安新县
130633    易县
130634    曲阳县
130635    蠡县
130636    顺平县
130637    博野县
130638    雄县
130681    涿州市
130682    定州市
130683    安国市
130684    高碑店市
130700    张家口市
130702    桥东区
130703    桥西区
130705    宣化区
130706    下花园区
130721    宣化县
130722    张北县
130723    康保县
130724    沽源县
130725    尚义县
130726    蔚县
130727    阳原县
130728    怀安县
130729    万全县
130730    怀来县
130731    涿鹿县
130732    赤城县
130733    崇礼县
130800    承德市
130802    双桥区
130803    双滦区
130804    鹰手营子矿区
130821    承德县
130822    兴隆县
130823    平泉县
130824    滦平县
130825    隆化县
130826    丰宁满族自治县
130827    宽城满族自治县
130828    围场满族蒙古族自治县
130900    沧州市
130902    新华区
130903    运河区
130921    沧县
130922    青县
130923    东光县
130924    海兴县
130925    盐山县
130926    肃宁县
130927    南皮县
130928    吴桥县
130929    献县
130930    孟村回族自治县
130981    泊头市
130982    任丘市
130983    黄骅市
130984    河间市
131000    廊坊市
131002    安次区
131003    广阳区
131022    固安县
131023    永清县
131024    香河县
131025    大城县
131026    文安县
131028    大厂回族自治县
131081    霸州市
131082    三河市
131100    衡水市
131102    桃城区
131121    枣强县
131122    武邑县
131123    武强县
131124    饶阳县
131125    安平县
131126    故城县
131127    景县
131128    阜城县
131181    冀州市
131182    深州市
}while(0)

 

上面的程序看起来没有问题,调试的时候也看不出什么错误,因为只是我一个人访问

可是当访问的用户多了之后〉1

dataGrid中的显示数据就会不正确!

这完全是因为使用了全局变量string,至于是否static与问题似乎无关

本来想请教朋友指点,自己实际动手发现确实是somestring的问题,于是不使用全局变量使用Session

 

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace test
{
 
/**//// <summary>
 
/// _default 的摘要说明。
 
/// </summary>

 public class _default : System.Web.UI.Page
 
{
  
protected System.Web.UI.WebControls.DropDownList DropDownList1;
  
protected System.Web.UI.WebControls.DataGrid DataGrid1;
  
protected System.Web.UI.WebControls.Label Label1;
  
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
  
protected System.Data.SqlClient.SqlCommand sqlSelectCommand2;
  
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
  
protected System.Data.SqlClient.SqlConnection sqlConnection1;
  
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
  
protected System.Web.UI.WebControls.Button Button1;
  
// static string somestring;
  
 
  
private void Page_Load(object sender, System.EventArgs e)
  
{
   
// 在此处放置用户代码以初始化页面
   
   
if(!IsPostBack)
   
{ Session.Add("somesting","qqq");
    
this.sqlSelectCommand1.Connection.Open();
   
    DropDownList1.DataSource
=this.sqlSelectCommand1.ExecuteReader();
    DropDownList1.DataTextField
="名称";
    DropDownList1.DataValueField
="代码";
    DropDownList1.DataBind();
    
this.sqlSelectCommand1.Connection.Close();
   }



  }


  Web 窗体设计器生成的代码
Web 窗体设计器生成的代码

  
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
  
{
   Session[
"somesting"]=DropDownList1.SelectedValue.ToString();
   Label1.Text
=Session["somesting"].ToString();
  }


  
private void Button1_Click(object sender, System.EventArgs e)
  
{
   
string tmp=Session["somesting"].ToString();
   
   
this.sqlSelectCommand2.CommandText = "SELECT 代码, 名称 FROM 行政区划 where left(代码,4)='"+tmp.Substring(0,4)+"'";
   
this.sqlSelectCommand2.Connection.Open();
   
   DataGrid1.DataSource
=this.sqlSelectCommand2.ExecuteReader();
      
   DataGrid1.DataBind();
   
this.sqlSelectCommand2.Connection.Close();
  
  }


  
 }

}

问题的测试好像已经结束,各用户之间不再互相影响

问题还要到单位,看看自己的程序再说

posted on 2005-01-19 02:44  大厨师  阅读(3606)  评论(8编辑  收藏  举报

导航