天天看點

Net對象記憶體表進行修改後更新到資料庫

代碼如下:

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.SqlClient;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

//實驗描述:實作對資料庫中的表進行字段數值前五位的截取後再更新會資料庫中;

public partial class 對dataset記憶體表進行操作 : System.Web.UI.Page

{

   protected void Page_Load(object sender, EventArgs e)

   {

       SqlConnection con = help.con();

       con.Open();

       SqlDataAdapter da = new SqlDataAdapter("select * from cs", con);

       SqlCommandBuilder sc = new SqlCommandBuilder(da);//對資料源與dataset更新相關聯,自動生成更新的語句

       DataSet ds = new DataSet();

       da.Fill(ds);

       for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

       {

           ds.Tables[0].Rows[i]["Fcate"] = Substr(Convert.ToString(ds.Tables[0].Rows[i]["Fcate"]),5);//對記憶體中的表進行修改

       }

       da.Update(ds);//更新到資料庫中

       this.GridView1.DataSource = ds;

       this.GridView1.DataBind();

   }

   private string Substr(string str,int length)

   {

       if (str.Length <= length)

       {

           return str;

       }

       string nwStr = str.Substring(0, length);

       nwStr = nwStr + "...";

       return nwStr;

    }

}