用ASP.NET编写网页连接SQL数据库时,在Page_Load里从数据库读出信息然后在点击按钮更新数据库不成功
protectedvoidPage_Load(objectsender,EventArgse){SqlConnectioncn=newSqlConnection(Comm...
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cn= new SqlConnection(Common.Class1.ConnectString);
string sql="select username,password,name,age,sex,phone,school,xueyuan,zhuanye,email from student where username='"+ this.Session["username"] +"'";
SqlDataAdapter da = new SqlDataAdapter(sql, cn);
DataSet ds=new DataSet();
cn.Open();
da.Fill(ds, "xinxi");
this.username.Text = ds.Tables["xinxi"].Rows[0]["username"].ToString();
this.password.Text = ds.Tables["xinxi"].Rows[0]["password"].ToString();
this.name.Text = ds.Tables["xinxi"].Rows[0]["name"].ToString();
this.age.Text = ds.Tables["xinxi"].Rows[0]["age"].ToString();
this.sex.Text = ds.Tables["xinxi"].Rows[0]["sex"].ToString();
this.phone.Text = ds.Tables["xinxi"].Rows[0]["phone"].ToString();
this.school.Text = ds.Tables["xinxi"].Rows[0]["school"].ToString();
this.xueyuan.Text = ds.Tables["xinxi"].Rows[0]["xueyuan"].ToString();
this.zhuanye.Text = ds.Tables["xinxi"].Rows[0]["zhuanye"].ToString();
this.email.Text = ds.Tables["xinxi"].Rows[0]["email"].ToString();
cn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
string username = this.username.Text;
string password = this.password.Text;
string name = this.name.Text;
string age = this.age.Text;
string sex = this.sex.Text;
string phone = this.phone.Text;
string school = this.school.Text;
string xueyuan = this.xueyuan.Text;
string zhuanye = this.zhuanye.Text;
string email = this.email.Text;
SqlConnection cn = new SqlConnection(Common.Class1.ConnectString);
string update="update student set username='"+ username +"',password='"+ password +"',name='"+ name +"',age='"+ age +"',sex='"+ sex +"',phone='"+ phone +"',school='"+ school +"',xueyuan='"+ xueyuan +"',zhuanye='"+ zhuanye +"',email='"+ email +"' where username='"+ this.Session["username"] +"'";
//string update = "update student set password='" + password + "',phone='" + phone + "'where username='" + this.Session["username"] + "'";
SqlCommand cmd = new SqlCommand(update, cn);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
Response.Redirect("学生登陆页面.aspx");
}
如果将Page_Load注释掉能正常更新但是同时运行就无法更新数据库为什么呢? 展开
{
SqlConnection cn= new SqlConnection(Common.Class1.ConnectString);
string sql="select username,password,name,age,sex,phone,school,xueyuan,zhuanye,email from student where username='"+ this.Session["username"] +"'";
SqlDataAdapter da = new SqlDataAdapter(sql, cn);
DataSet ds=new DataSet();
cn.Open();
da.Fill(ds, "xinxi");
this.username.Text = ds.Tables["xinxi"].Rows[0]["username"].ToString();
this.password.Text = ds.Tables["xinxi"].Rows[0]["password"].ToString();
this.name.Text = ds.Tables["xinxi"].Rows[0]["name"].ToString();
this.age.Text = ds.Tables["xinxi"].Rows[0]["age"].ToString();
this.sex.Text = ds.Tables["xinxi"].Rows[0]["sex"].ToString();
this.phone.Text = ds.Tables["xinxi"].Rows[0]["phone"].ToString();
this.school.Text = ds.Tables["xinxi"].Rows[0]["school"].ToString();
this.xueyuan.Text = ds.Tables["xinxi"].Rows[0]["xueyuan"].ToString();
this.zhuanye.Text = ds.Tables["xinxi"].Rows[0]["zhuanye"].ToString();
this.email.Text = ds.Tables["xinxi"].Rows[0]["email"].ToString();
cn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
string username = this.username.Text;
string password = this.password.Text;
string name = this.name.Text;
string age = this.age.Text;
string sex = this.sex.Text;
string phone = this.phone.Text;
string school = this.school.Text;
string xueyuan = this.xueyuan.Text;
string zhuanye = this.zhuanye.Text;
string email = this.email.Text;
SqlConnection cn = new SqlConnection(Common.Class1.ConnectString);
string update="update student set username='"+ username +"',password='"+ password +"',name='"+ name +"',age='"+ age +"',sex='"+ sex +"',phone='"+ phone +"',school='"+ school +"',xueyuan='"+ xueyuan +"',zhuanye='"+ zhuanye +"',email='"+ email +"' where username='"+ this.Session["username"] +"'";
//string update = "update student set password='" + password + "',phone='" + phone + "'where username='" + this.Session["username"] + "'";
SqlCommand cmd = new SqlCommand(update, cn);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
Response.Redirect("学生登陆页面.aspx");
}
如果将Page_Load注释掉能正常更新但是同时运行就无法更新数据库为什么呢? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询