public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
       // if (!IsPostBack)
       // {
       //    this.bind();
       // }
    }    public OracleConnection Getconnection()
    {
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        OracleConnection myConn = new OracleConnection( myStr );
        return myConn;
    }    //数据查询    protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.TextBox1.Text != "")
        {
            OracleConnection myConn = Getconnection();
            myConn.Open();
            string sqlStr = "select * from SH.JIAOYIXINXI where Name=:Name";
            OracleCommand myCmd = new OracleCommand(sqlStr, myConn);
            myCmd.Parameters.Add("Name", OracleType.VarChar, 20).Value = this.TextBox1.Text.Trim();
            
            OracleDataAdapter myDa = new OracleDataAdapter(myCmd);
            DataSet myDs = new DataSet();
            myDa.Fill(myDs);
            if (myDs.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = myDs;
                GridView1.DataBind();
            }
            else
            {
                Response.Write("<script>alert('没有相关记录') </script>");
            }
            myDa.Dispose();
            myDs.Dispose();
            myConn.Close();
        }
    }
        
        //数据绑定    protected void bind()
         {
            OracleConnection myconn = Getconnection();
            myconn.Open();
            string sqlstr = "select * from SH.KEHUXINXI";
            OracleDataAdapter myda = new OracleDataAdapter(sqlstr,myconn);
            DataSet myds = new DataSet();
            myda.Fill(myds);
            GridView2.DataSource = myds;
            GridView2.DataKeyNames = new string[] { "姓名" };
            GridView2.DataBind();
            myds.Dispose();
            myds.Dispose();
            myconn.Close();
         }    //数据插入    protected void Button2_Click(object sender, EventArgs e)
        {
            if (this.TextBox2.Text != "")
            {
                OracleConnection myConn = Getconnection();
                myConn.Open();
                string sqlstr = "insert into SH.KEHUXINXI(姓名) values(" + this.TextBox2.Text.Trim() + ")";
                OracleCommand myCmd = new OracleCommand(sqlstr, myConn);
                myCmd.ExecuteNonQuery();
                myConn.Close();
                //this.bind();
                Response.Write("<script>alert('插入成功!') </script>");
            }
            else
                //this.bind();
                Response.Write("<script>alert('插入失败!') </script>");
    }    //绑定Row编辑    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView2.EditIndex = e.NewEditIndex;
            this.bind ();
        }   //数据更新
    
    protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
        int 姓名 = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value.ToString());
        string CName = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[2].Controls[0] )).Text.ToString();
        string sqlstr = "update SH.KEHUXINXI set 姓名='" + CName + "'" ;
        OracleConnection myconn = new OracleConnection();
        myconn.Open();
        OracleCommand mycmd = new OracleCommand(sqlstr, myconn);
        mycmd.ExecuteNonQuery();
        mycmd.Dispose();
        myconn.Close();
        GridView2.EditIndex = -1;
        this.bind();        }
    
 }
    
   哪位高人给我说一下为什么我的这个数据更新不了?是哪里有问题么?为什么插入语句都是正常的,而更新的时候就显示除非使用updatecommand,否则datasourse2不允许更新数据,满分求救!急,谢谢!

解决方案 »

  1.   

    为什么插入语句都是正常的,而更新的时候就显示除非使用updatecommand,否则datasourse2不允许更新数据,满分求救!急,谢谢!你的插入语句也是用命令实现的呀!
    string sqlstr = "insert into SH.KEHUXINXI(姓名) values(" + this.TextBox2.Text.Trim() + ")";
      OracleCommand myCmd = new OracleCommand(sqlstr, myConn);
      myCmd.ExecuteNonQuery();然后你的更新语句这么写,也没有问题呀!也原因需要命令来执行:
    string sqlstr = "update SH.KEHUXINXI set 姓名='" + CName + "'" ;
      OracleConnection myconn = new OracleConnection();
      myconn.Open();
      OracleCommand mycmd = new OracleCommand(sqlstr, myconn);
      mycmd.ExecuteNonQuery();
      

  2.   

    眼拙,还真没发现啥大问题,要不贴个出错信息看看?再稍微提个建议Connection维护一个可能好点,用单例模式