using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("server=localhost;database=xitong;uid=sa;pwd=123");
        con.Open();
        string username = this.TextBox2.Text.Trim();
      
        string strSql = "SELECT * FROM yonghu WHERE name = '" + username + "'";
        SqlCommand ob = new SqlCommand(strSql, con);
        SqlDataReader dt = ob.ExecuteReader();
        if (dt.Read())
        {            Response.Write("<script>alert('用户名不可用!');</script>");
        
        }
        else
        {
            dt.Close();
            string a = this.T2.Value;
            string b = this.T3.Value;
            if (a != b)
            {
                Response.Write("<script>alert('两次密码输入不相同!');</script>");
            }
            else
            {
                string c = this.T4.Value;
          
                string str = "insert into yonghu (name,shenfenzhenghao,password)values('" + TextBox2.Text + "','" +c + "','" + a  + "')";
                ob.ExecuteNonQuery(); 
                Response.Write("<script>alert('欢迎您成为本系统的项目申报人!');</script>");
                Response.Redirect("mima.aspx");
                Response.Write("<script>history.go(-1);</script>");
             
            }
       
        }
    }
}

解决方案 »

  1.   

    string str = "insert into yonghu (name,shenfenzhenghao,password)values('" + TextBox2.Text + "','" +c + "','" + a + "')";
      ob.ExecuteNonQuery(); 
    最后这段怎么可能会执行呢,你只是写了sql语句 但是并没有执行它,
    和上面一样的
    sqlcommand comm=new sqlcommand(str,con);
    int ret =comm.ExecuteNonQuery();
    if(ret>0)
    else
      

  2.   

    最后注意要关闭sql;con.close();
    如果不想这样操作 你可以在用using()
      

  3.   

    楼主的结帖率好像不够高啊,
    public partial class _Default : System.Web.UI.Page 
    {
      protected void Page_Load(object sender, EventArgs e)
      {  }
      protected void Button1_Click(object sender, EventArgs e)
      {
      SqlConnection con = new SqlConnection("server=localhost;database=xitong;uid=sa;pwd=123");
      con.Open();
      string username = this.TextBox2.Text.Trim();
       
      string strSql = "SELECT * FROM yonghu WHERE name = '" + username + "'";
      SqlCommand ob = new SqlCommand(strSql, con);
      SqlDataReader dt = ob.ExecuteReader();
      if (dt.Read())
      {  Response.Write("<script>alert('用户名不可用!');</script>");
    con.Close();
       
      }
      else
      {
      dt.Close();
    con.Close();
      string a = this.T2.Value;
      string b = this.T3.Value;
      if (a != b)
      {
      Response.Write("<script>alert('两次密码输入不相同!');</script>");
      }
      else
      {
      string c = this.T4.Value;
       SqlConnection con1 = new SqlConnection("server=localhost;database=xitong;uid=sa;pwd=123");
      con1.Open();
      string str = "insert into yonghu (name,shenfenzhenghao,password)values('" + TextBox2.Text + "','" +c + "','" + a + "')";
      SqlCommand comm= new SqlCommand(str, con1);
    int ret= comm.ExecuteNonQuery(); 
    con1.Close();
    if(ret>0){
      Response.Write("<script>alert('欢迎您成为本系统的项目申报人!');</script>");
      Response.Redirect("mima.aspx");
      Response.Write("<script>history.go(-1);</script>");
       }else{con1.Close();}
      }
       
      }
      }
    }这代码写的真是乱遭了,哎,,,,  
      

  4.   

    ckmin520 说的很对,还有一个小建议 就是楼主写的数据库相关操作可以单独放在一个类里面,你这样直接写,代码有点乱,可读性也不太强
      

  5.   

    嘿嘿,听取了大家的意见,和数据库连上了,但是Response.Write("<script>alert('欢迎您成为本系统的项目申报人!');</script>");
    这句怎么不执行呢?
      

  6.   


    不对。。是已经执行了。不过执行的是在当前页面
    Response.Write("<script>alert('欢迎您成为本系统的项目申报人!');</script>");
    然后你又跳转到 mima.aspx 页面了。。怎么可能看到。。
    Response.Redirect("mima.aspx"); 
      

  7.   

    if(ret>0){
      Response.Write("<script>alert('欢迎您成为本系统的项目申报人!');</script>");
      Response.Redirect("mima.aspx");
      Response.Write("<script>history.go(-1);</script>");
      }else{con1.Close();}
      }把里面的换成 Response.Write("<script language='javascript'>alert('欢迎您成为本系统的项目申报人');winodw.location.href='mima.aspx'</script>");
    楼主 该结贴了  给分吧
      

  8.   

     其实你这个 sqlcommand 这个还没有配置完
    他的几个属性  cmd.CommandText = Text;这个属性得告诉sql你要执行什么是存储过程还是 sql文本
    你这里是 文本Text