<P>我在公司怎么也无法发布过长的帖子,所以发了几次都没发完整,具体问题如下:</P>
<P>4.4.3 Session对象在实际开发中的应用中的问题</P>
<P> </P>
<P>Session对象实例(4.4.3小节),我的Default.aspx.cs文件如下:(其中Uid=sa;Pwd=ahjob12308是我装SQL2000时的sa用户和密码)</P>
<P>using System;<BR>using System.Configuration;<BR>using System.Data;<BR>using System.Linq;<BR>using System.Web;<BR>using System.Web.Security;<BR>using System.Web.UI;<BR>using System.Web.UI.HtmlControls;<BR>using System.Web.UI.WebControls;<BR>using System.Web.UI.WebControls.WebParts;<BR>using System.Xml.Linq;<BR>using System.Data.SqlClient;</P>
<P>public partial class _Default : System.Web.UI.Page <BR>{<BR> protected void Page_Load(object sender, EventArgs e)<BR> {</P>
<P> }<BR> protected void Button1_Click(object sneder, EventArgs e)<BR> {<BR> //建立与数据库的连接<BR> SqlConnection sqlconn = new SqlConnection("Data Source=(local);Database=Northwind;Uid=sa;Pwd=ahjob12308");<BR> //打开数据库连接<BR> sqlconn.Open();<BR> //创建一个命令对象<BR> SqlCommand sqlcom = sqlconn.CreateCommand();<BR> sqlcom.CommandText = "select count(*) from Region where RegionID=" + TextBox1.Text<BR> + "and RegionDescription=" + TextBox2.Text + "";<BR> //获取SQL语句的值,强制转换成数值类型<BR> int count=Convert.ToInt32(sqlcom.ExecuteScalar());<BR> //判断登陆是否成功<BR> if (count > 0)<BR> {<BR> Session["name"] = TextBox1.Text;<BR> Session["pwd"] = TextBox2.Text;<BR> Page.Response.Redirect("Default2.aspx");<BR> }<BR> else<BR> {<BR> Response.Write("<script language=javascript>alert('用户名或密码有误!');location='javascript:history.go(-1)'</Script>");<BR> }<BR> }<BR>}</P>
<P><BR>另外,Default2.aspx.cs的文件如下:</P>
<P>using System;<BR>using System.Collections;<BR>using System.Configuration;<BR>using System.Data;<BR>using System.Linq;<BR>using System.Web;<BR>using System.Web.Security;<BR>using System.Web.UI;<BR>using System.Web.UI.HtmlControls;<BR>using System.Web.UI.WebControls;<BR>using System.Web.UI.WebControls.WebParts;<BR>using System.Xml.Linq;</P>
<P>public partial class Default2 : System.Web.UI.Page<BR>{<BR> protected void Page_Load(object sender, EventArgs e)<BR> {<BR> if (!IsPostBack)<BR> {<BR> Response.Write("用Session对象传的用户名为:" + Session["name"].ToString()<BR> + "<br>" + "密码为:" + Session["pwd"].ToString());<BR> }<BR> }<BR>}</P>
<P> </P>
<P>运行以后,int count=Convert.ToInt32(sqlcom.ExecuteScalar());变成绿色,并且<BR>弹出对话框,老是报</P>
<P><BR>用户代码未处理SqlException<BR>第 1 行: 'RegionDescription' 附近有语法错误。</P>
<P> </P>
<P>PS:我不知道怎么在SQL2000里添加这个程序里相应的检验数据,大大们能不能稍微说一下.<BR></P>