一:用数据集制作:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingDataSet1TableAdapters;
usingSystem.Data;
publicpartialclassDefault2:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
UsersTableAdapteradapter=newUsersTableAdapter();
DataSet1.UsersDataTabletable=newDataSet1.UsersDataTable();
adapter.Fill(table);
//table[0].name="张";//修改
//table[3].Delete();//删除
//DataSet1.UsersRowrow=table.NewUsersRow();//增加
//row.uid="004";
//row.name="保定";
//row.password="123";
//row.power=2;
//row._lock=false;
//table.Rows.Add(row);
DataRow[]rows=table.Select("name='张三'");//查找
Response.Write(rows[0]["uid"]+"|"+rows[0]["name"].ToString()+"|"+rows[0]["password"]+"|"+rows[0]["power"]+"|"+rows[0]["lock"]);
adapter.Update(table);//这句话必须有否则只能更新视图不能更新数据库
this.GridView1.DataSource=table;
this.GridView1.DataBind();
}
}
二:用DataSet做:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
publicpartialclass_Default:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
DataSetds=newDataSet();
stringstr=ConfigurationManager.ConnectionStrings["sqlstring"].ConnectionString;
using(SqlConnectionsqlcnn=newSqlConnection(str))
{
SqlCommandsqlcmm=sqlcnn.CreateCommand();
sqlcmm.CommandText="select*fromUsers";
SqlDataAdapteradapter=newSqlDataAdapter(sqlcmm);
adapter.Fill(ds,"Users");
//ds.Tables["Users"].Rows[0].Delete();//删除
//ds.Tables["Users"].Rows[1]["name"]="李?四?";//修改
//DataRowdr=ds.Tables[0].NewRow();//增加
//dr["uid"]=0007;
//dr["name"]="赵?六ⅷ?;
//dr["password"]=123;
//dr["power"]=0;
//dr["lock"]=false;
//ds.Tables[0].Rows.Add(dr);
//DataRow[]rows=ds.Tables[0].Select("name='张三'");
//查找
//Response.Write(rows[0]["name"].ToString()+rows[0]["password"]);
//DataRow[]rows=t.Select("Dname='"+TextBox3.Text.Trim()+"'","idDESC");
SqlCommandBuilderbuilder=newSqlCommandBuilder(adapter);
adapter.Update(ds,"Users");//这句话必须有否则只能更新视图不能更新数据库
}
this.GridView1.DataSource=ds.Tables[0];
this.GridView1.DataBind();
}
}