ADO.NET DataSet

DataSet技术 DataTable DataView
// *****************************************************************************/
//  在数据库vls 中有个 PROVINCE表 其中字段有 PROVINCE_ID/ PROVINCE_CDE/ PROVINCE_NAME ××包含中国34省,市,自治区
// *****************************************************************************/
From 与ASP.NET 用法一样
1 .绑定DataGird
string  constr = " user id=sa;password=168168;initial catalog=VLS;data source=yu;Connect Timeout=30 " ;
SqlConnection mycon
= new  SqlConnection(constr);
   
string  sqlstr = " select * from PROVINCE " ;
SqlDataAdapter dap
= new  SqlDataAdapter(sqlstr,mycon);
//  也可以用下面3句 调用 SqlCommand组件
// SqlCommand mycomd=new SqlCommand(sqlstr,mycon);
// SqlDataAdapter dap=new SqlDataAdapter();
// dap.SelectCommand=mycomd; 

mycon.Open();
DataSet ds
= new  DataSet();
dap.Fill(ds,
" PROVINCE " );
mycon.Close();

this .DataGrid1.DataSource = ds.Tables[ 0 ].DefaultView;
this .DataGrid1.DataBind();
2 .增加
string  constr = " user id=sa;password=168168;initial catalog=VLS;data source=yu;Connect Timeout=30 " ;
SqlConnection mycon
= new  SqlConnection(constr);
  
string  sqlstr = " select * from PROVINCE " ;
SqlCommand mycomd
= new  SqlCommand(sqlstr,mycon);
mycon.Open();
SqlDataAdapter myAdapter
= new  SqlDataAdapter();
myAdapter.SelectCommand
= mycomd;
DataSet ds
= new  DataSet();
myAdapter.Fill(ds,
" PROVINCE " );
// 增加
for ( int  i = 0 ;i < 34 ;i ++ )
{
DataRow drw
=ds.Tables[0].NewRow();
drw[
"PROVINCE_ID"]="temp"+Convert.ToString(i);
drw[
"PROVINCE_NAME"]="你好们";
ds.Tables[
0].Rows.Add(drw);
}

SqlCommandBuilder myCommandBuilder 
=   new  SqlCommandBuilder(myAdapter);
myAdapter.Update(ds,
" PROVINCE " ); 
ds.Tables[
0 ].AcceptChanges();
mycon.Close();
3 .删除 修改  // ****************删除 修改记录的 表一定要有主键
string  constr = " user id=sa;password=168168;initial catalog=VLS;data source=yu;Connect Timeout=30 " ;
SqlConnection mycon
= new  SqlConnection(constr);
   
string  sqlstr = " select * from PROVINCE " ;
SqlCommand mycomd
= new  SqlCommand(sqlstr,mycon);
mycon.Open();
SqlDataAdapter myAdapter
= new  SqlDataAdapter();
myAdapter.SelectCommand
= mycomd;
DataSet ds
= new  DataSet();
myAdapter.Fill(ds,
" PROVINCE " );
    
DataRow []drws
= ds.Tables[ 0 ].Select( " PROVINCE_ID like 'temp%' " );
// 删除 
foreach (DataRow drw  in  drws)
{
Response.Write(drw[
"PROVINCE_NAME"].ToString()+" ");
drw.Delete();
}

// 修改
foreach (DataRow drw  in  ds.Tables[ 0 ].Rows)
{
Response.Write(drw[
"PROVINCE_NAME"].ToString()+" ");
drw[
"PROVINCE_CDE"]="M2";
}

SqlCommandBuilder myCommandBuilder 
=   new  SqlCommandBuilder(myAdapter);
myAdapter.Update(ds,ds.Tables[
0 ].TableName); 
ds.Tables[
0 ].AcceptChanges();
mycon.Close();

DataTable
1 .新建表绑定
DataTable dt
= new  DataTable();
DataRow dr;
dt.Columns.Add(
new  DataColumn( " id " , typeof (Int32)));
dt.Columns.Add(
new  DataColumn( " text " , typeof ( string )));      
for ( int  i = 0 ;i < 10 ;i ++ )
{
dr
=dt.NewRow();
dr[
0]=i;
dr[
1]="sds"+i.ToString();
dt.Rows.Add(dr);
}

this .DataGrid1.DataSource = dt.DefaultView;
this .DataGrid1.DataBind();
2 . // DataTable 经常用到的 select属性 用来 判断重复
DataTable dt = new  DataTable();DataRow dr;
dt.Columns.Add(
new  DataColumn( " id " , typeof ( string )));
dt.Columns.Add(
new  DataColumn( " text " , typeof ( string )));
for ( int  i = 0 ;i < 10 ;i ++ )
{
dr
=dt.NewRow();
dr[
0]="ff"+i.ToString();;
dr[
1]="sds"+i.ToString();
dt.Rows.Add(dr);
}

string  str = " id='ff3' and text='sds3' " ;
// 模糊查询  // string str=" text like 's%'";
DataRow[] drws = dt.Select(str);
if (drws.Length > 0 )
{Response.Write("有重复记录!");}
else {Response.Write("无重复记录!");}

DataView
// DataView 就是 筛选,过滤后的DataTable
1 .筛选
DataTable dt
= new  DataTable();DataRow dr;
dt.Columns.Add(
new  DataColumn( " id " , typeof (Int32)));
dt.Columns.Add(
new  DataColumn( " text " , typeof ( string )));
for ( int  i = 0 ;i < 10 ;i ++ ) {
dr
=dt.NewRow();
dr[
0]=i;
dr[
1]="sds"+i.ToString();
dt.Rows.Add(dr);    }

dt.DefaultView.RowFilter
= " id=5 " ;
this .DataGrid1.DataSource = dt.DefaultView;
this .DataGrid1.DataBind();
2 .排序
DataTable dt
= new  DataTable();DataRow dr;
dt.Columns.Add(
new  DataColumn( " id " , typeof (Int32)));
dt.Columns.Add(
new  DataColumn( " text " , typeof ( string )));
for ( int  i = 0 ;i < 10 ;i ++ )
{
dr
=dt.NewRow();
dr[
0]=i; 
dr[
1]="sds"+i.ToString();
dt.Rows.Add(dr);
}

dt.DefaultView.Sort
= " id desc " ;
this .DataGrid1.DataSource = dt.DefaultView;
this .DataGrid1.DataBind();

你可能感兴趣的:(.net)