dataset的增加,刪除,,查找

 using System;
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)
    {
       DataSet ds = new DataSet();
        string str = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        using (SqlConnection con = new SqlConnection(str))
        {
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = "select * from pros";
            SqlDataAdapter sqlda = new SqlDataAdapter(cmd);
            con.Open();
            sqlda.Fill(ds);
            //修改記錄
           // DataRow[] row1 = ds.Tables[0].Select("name='"+txtname.Text+"'");
           // foreach (var item in row1)
           // {


              
           //     item["name"] = this.txtname.Text;
           //     item["price"] = this.txtnum.Text;
           //     item["photoPath"] = this.txtphoto.Text;
              
           // }
           // 添加一行數據

           // DataRow row = ds.Tables[0].NewRow();
           // row["name"] = this.txtname.Text;
           // row["price"] = this.txtnum.Text;
           // row["photoPath"] = this.txtphoto.Text;
           // ds.Tables[0].Rows.Add(row);
  
           //刪除數據
            DataRow[] row2 = ds.Tables[0].Select("name='" + txtname.Text + "'");

            foreach (var items in row2)
            {
                items.Delete();
            }

       
       
            SqlCommandBuilder builder = new SqlCommandBuilder(sqlda);
            sqlda.Update(ds);
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }
    }
   
}

 

 

保持初學者的心態!!!

發佈了53 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章