public class fangfa
{
//
//TODO: 在此处添加构造函数逻辑
//
    SqlConnection conn = null;//连接数据库的对象
    //构造函数链接数据库 下
    public fangfa()
    {
        if(conn==null)
        {
            conn=new SqlConnection();
            conn.ConnectionString=@"Data Source=N\SQLEXPRESS;Initial Catalog=feiteng;Integrated Security=True";//连接数据库的字符串
        }
        if(conn.State==ConnectionState.Closed)
        {
            conn.Open();//打开数据库的连接
        }    }
    //从数据库查找数据的方法
    public DataSet query(string sql)
    {
        DataSet ds=new DataSet();//dataset是表的集合
        SqlDataAdapter da=new SqlDataAdapter(sql,conn);//从数据库中查找
        da.Fill(ds);//将数据填充到dataset
        conn.Close();//关闭连接
        return ds;//返回结果
    }
    //下边是更新数据库
    public int updata(string sql)
    {
        SqlCommand cmd = new SqlCommand();//表示要对数据源执行的SQL语句或存储过程
        cmd.CommandText = sql;//设置命令文本
        cmd.CommandType = CommandType.Text;//设置命令的类型
        cmd.Connection=conn;//设置命令的连接
        int x=cmd.ExecuteNonQuery();//执行sql语句
        conn.Close();//关闭连接
        return x;//返回影响行数
    }
    //下边方法是关闭数据库连接
    public void connclose()
    {
        if(conn.State==ConnectionState.Open)
        {//判断数据库连接状态 如果打开 就将他关闭
            conn.Close();
        }
    }   

}protected void Page_Load(object sender, EventArgs e)
    {
        string source = @"Data Source=N\SQLEXPRESS;Initial Catalog=feiteng;Integrated Security=True";
        SqlConnection conn = new SqlConnection(source);
        conn.Open();
        string select = "select name from gongsijianjie";
        SqlCommand cmd = new SqlCommand(select,conn);
        SqlDataReader reader = cmd.ExecuteReader();
        if(reader.Read())
        {
            TextBox1.Text=reader["name"].ToString();
        }
        conn.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if(this.TextBox1.Text=="")
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(),"ok","alert('不能为空')",true);
        }
        string name = this.TextBox1.Text;
        string sql = string.Format("update gongsijianjie set [name]='{0}'",name);
        int y = new fangfa().updata(sql);
        if (y > 0)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ok", "alert('修改成功!');", true);
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ok", "alert('修改失败!');", true);
        }
    }

解决方案 »

  1.   

    检查你的逻辑先,
    看看你的SQL语句到底执行没!!记住,一定要调试!
      

  2.   

    protected void Page_Load(object sender, EventArgs e)
      {
      string source = @"Data Source=N\SQLEXPRESS;Initial Catalog=feiteng;Integrated Security=True";
      SqlConnection conn = new SqlConnection(source);
      conn.Open();
      string select = "select name from gongsijianjie";
      SqlCommand cmd = new SqlCommand(select,conn);
      SqlDataReader reader = cmd.ExecuteReader();
      if(reader.Read())
      {
      TextBox1.Text=reader["name"].ToString();
      }
      conn.Close();
      }
    改成
    protected void Page_Load(object sender, EventArgs e)
      {
       if(IsPostBack)return;
      string source = @"Data Source=N\SQLEXPRESS;Initial Catalog=feiteng;Integrated Security=True";
      SqlConnection conn = new SqlConnection(source);
      conn.Open();
      string select = "select name from gongsijianjie";
      SqlCommand cmd = new SqlCommand(select,conn);
      SqlDataReader reader = cmd.ExecuteReader();
      if(reader.Read())
      {
      TextBox1.Text=reader["name"].ToString();
      }
      conn.Close();
      }