ASP.NET學習筆記之增刪改查操作

因爲之前有個CSDN的賬號但是後來賬號丟了,於是只能另起爐竈,今天總有找回了丟失已久的登錄祕密,不知道有沒有辦法將兩個博客合併,只能手動複製黏貼以前的博文過來。。。悲慘

源地址:http://blog.csdn.net/sxfenglei/article/details/5431181 該博客不再用了

現在看看當年寫的代碼 真幼稚呵呵 轉過來紀念下當年的稚嫩,哈哈

/// <summary> ­
/// Page.Load 加載時 使用!IsPostBack 判斷是否回顯 ­
/// </summary> ­
  if (!IsPostBack)// !IsPostBack 獲取一個值,該值指示該頁是否正爲響應客戶端回發而加載,或者它是否正被首次加載和訪問 ­
//==================================連接數據庫  增刪改查================================== ­
/// <summary> ­
/// 在 web.config中配置數據庫字符串 ­
/// </summary> ­
  <appSettings> ­
   <add key="ConnectionString" value="Data Source=ASUS/SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True"/> ­
  </appSettings> ­
/// <summary> ­
/// 獲取 Web.Config 配置文件中與數據庫連接的字符串 ­
/// </summary> ­
  public SqlConnection GetConnection() ­
  { ­
   string sqlconString = ConfigurationManager.AppSettings["ConnectionString"].ToString(); ­
   SqlConnection con = new SqlConnection(sqlconString); ­
   return con; ­
  } ­
/// <summary> ­
/// 編寫 填充方法 ­
/// </summary> ­
  private void fillDataGridView() ­
  { ­
   //將方法賦值給con  等同於SqlConnection con = new SqlConnection(sqlconString); ­
   SqlConnection con = GetConnection(); ­
   con.Open(); ­
   //拼SQL語句 ­
   string sql = "select * from yuanGong"; ­
   //創建 Command ­
   SqlCommand com = new SqlCommand(sql, con); ­
   //創建 DataAdapter ­
   SqlDataAdapter dataAdapter = new SqlDataAdapter(com); ­
   //創建 DataSet ­
   DataSet dataSet = new DataSet(); ­
   //填充 DataSet ­
   dataAdapter.Fill(dataSet); ­
   //填充 GridView 控件 ­
   GridView1.DataSource = dataSet; ­
   //將數據綁定到 GridView 控件 ­
   GridView1.DataBind(); ­
   //釋放資源 ­
   dataAdapter.Dispose(); ­
   dataSet.Dispose(); ­
   con.Close(); ­
  } ­
/// <summary> ­
/// 按照條件查詢數據 ­
/// </summary> ­
  //如果文本框不爲空(進行查詢) 如果文本框爲空(填充所有數據) ­
        if(this.TextBox1.Text!="") ­
        { ­
            //創建 Connection 對象 ­
            SqlConnection con = GetConnection(); ­
            con.Open(); ­
            //拼SQL ­
            string sql = "select * from yuanGong where name=@name"; ­
            //創建 Command 對象 ­
            SqlCommand com = new SqlCommand(sql,con); ­
            // ­
            com.Parameters.Add("@name", SqlDbType.VarChar, 20).Value = this.TextBox1.Text.Trim(); ­
            //創建 DataAdapter 對象 ­
            SqlDataAdapter dataAdpater = new SqlDataAdapter(com); ­
            //創建 DataSet 對象 ­
            DataSet dataSet = new DataSet(); ­
            //填充 dataSet ­
            dataAdpater.Fill(dataSet); ­
            //判斷是否查到數據 ­
            if (dataSet.Tables[0].Rows.Count > 0) ­
            { ­
                //填充 GridView ­
                GridView1.DataSource = dataSet; ­
                //綁定數據到 GridView ­
                GridView1.DataBind(); ­
            } ­
            else ­
            { ­
                Response.Write("<script>alert('報告馮磊老大,沒有相關記錄!')</script>"); ­
            } ­
            //釋放資源佔用 ­
            dataAdpater.Dispose(); ­
            dataSet.Dispose(); ­
            con.Close(); ­
        }else{ ­
            this.fillDataGridView();//調用自定義方法 填充所有數據 ­
        } ­
/// <summary>添加數據 ­
/// 添加數據 ­
/// </summary> ­
  if(TextBox1.Text.Trim()!="") ­
   { ­
    SqlConnection con = GetConnection(); ­
    string sql = "insert into yuanGong (Name) values ( '"+this.TextBox1.Text.Trim()+" ')";//特別聲明:‘this.TextBox1.Text.Trim()’ 應放在單引號中 ­
    SqlCommand com = new SqlCommand(sql,con); ­
    con.Open(); ­
    com.ExecuteNonQuery(); ­
    con.Close(); ­
    this.fillDataGridView(); ­
   }else{ ­
    this.fillDataGridView(); ­
   } ­
/// <summary>修改數據 ­
/// 編輯 ­
/// </summary> ­
/// <param name="sender"></param> ­
/// <param name="e"></param> ­
  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) ­
  { ­
   // ­
   GridView1.EditIndex = e.NewEditIndex; ­
   //重新加載 GridView ­
   this.fillDataGridView(); ­
  } ­
/// <summary>更新 按鍵 GridView RowUpdating 方法 ­
/// 更新 ­
/// </summary> ­
/// <param name="sender"></param> ­
/// <param name="e"></param> ­
  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) ­
  { ­
   //拿到值 ­
   int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()); ­
   string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString(); ­
   string age = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString(); ­
   string gongZi = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString(); ­
   string jiGuan = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString(); ­
   //拼SQL ­
   string sql = string.Format("update yuanGong set Name='{0}',Age='{1}',gongZi='{2}',jiGuan='{3}' where ID='{4}'", name,age,gongZi,jiGuan,id); ­
   //創建 connection 對象 ­
   SqlConnection con = GetConnection(); ­
   //打開數據庫連接 ­
   con.Open(); ­
   //創建 command 對象 ­
   SqlCommand com = new SqlCommand(sql,con); ­
   //執行SQL ­
   com.ExecuteNonQuery(); ­
   //釋放資源 ­
   com.Dispose(); ­
   con.Close(); ­
   // ­
   GridView1.EditIndex = -1; ­
   //重新加載 GridView ­
   this.fillDataGridView(); ­
  } ­
/// <summary>取消按鍵 GridView 的 RowCancelingEdit 方法 ­
/// 取消 ­
/// </summary> ­
/// <param name="sender"></param> ­
/// <param name="e"></param> ­
  protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) ­
  { ­
   GridView1.EditIndex = -1; ­
   //重新加載 GridView ­
   this.fillDataGridView(); ­
  } ­
/// <summary>刪除數據 ­
/// 刪除數據 ­
/// </summary> ­
/// <param name="sender"></param> ­
/// <param name="e"></param> ­
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) ­
    { ­
        //拿到 ID ­
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()); ­
        //拼SQL ­
        string sqlStr = "delete from yuanGong where ID=" + id; ­
        SqlConnection myConn = GetConnection(); ­
        myConn.Open(); ­
        SqlCommand myCmd = new SqlCommand(sqlStr, myConn); ­
        myCmd.ExecuteNonQuery(); ­
        myCmd.Dispose(); ­
        myConn.Close(); ­
        GridView1.EditIndex = -1; ­
        this.fillDataGridView(); ­
    } ­
/// <summary>彈出窗口詢問是否刪除 ­
    /// 彈出窗口詢問是否刪除 ­
/// </summary> ­
/// <param name="sender"></param> ­
/// <param name="e"></param> ­
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) ­
    { ­
        // ­
        if (e.Row.RowType == DataControlRowType.DataRow) ­
        { ­
            ((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "return confirm('馮磊老大你確定要刪除我嗎?')"); ­
        } ­
    } ­


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章