protected
void
Page_Load(
object
sender, EventArgs e)
{
DataTable myTb = new DataTable( " auto " );
DataColumn myColid;
myColid = new DataColumn();
myColid.DataType = System.Type.GetType( " System.Int32 " );
myColid.ColumnName = " id " ;
myColid.ReadOnly = true ;
myTb.Columns.Add(myColid);
DataColumn myColname = new DataColumn();
myColname.DataType = System.Type.GetType( " System.String " );
myColname.ColumnName = " Name " ;
myTb.Columns.Add(myColname);
DataColumn myColtimer = new DataColumn();
myColtimer.DataType = System.Type.GetType( " System.DateTime " );
myColtimer.ColumnName = " Timer " ;
myTb.Columns.Add(myColtimer);
/// / 主键的创建
// DataColumn[] PrimaryKeyColumns = new DataColumn[1];
// PrimaryKeyColumns[0] = myTb.Columns["id"];
// myTb.PrimaryKey = PrimaryKeyColumns;
myTb.PrimaryKey = new DataColumn[] { myColid };
DataRow myDr;
myDr = myTb.NewRow();
myDr[ " id " ] = 1 ;
myDr[ " Name " ] = " Name " ;
myDr[ " Timer " ] = DateTime.Now.AddSeconds( 10 );
myTb.Rows.Add(myDr);
myDr = myTb.NewRow();
myDr[ " id " ] = 11 ;
myDr[ " Name " ] = " Name11 " ;
myDr[ " Timer " ] = DateTime.Now.AddSeconds( 8 );
myTb.Rows.Add(myDr);
myDr = myTb.NewRow();
myDr[ " id " ] = 111 ;
myDr[ " Name " ] = " Name111 " ;
myDr[ " Timer " ] = DateTime.Now.AddSeconds( 5 );
myTb.Rows.Add(myDr);
myDr = myTb.NewRow();
myDr[ " id " ] = 2 ;
myDr[ " Name " ] = " Name2 " ;
myDr[ " Timer " ] = DateTime.Now.AddSeconds( 3 );
myTb.Rows.Add(myDr);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string sortOrder = " Timer asc " ;
// foreach (DataColumn dc in myTb.Columns)
// {
// // sb.Append(dc.ColumnName+"<br/>");
// }
// foreach (DataRow dr in myTb.Rows)
// {
// foreach (DataColumn dc in myTb.Columns)
// {
// sb.Append(dr[dc] + "|");
// }
// sb.Append("<br/>");
// }
myTb = DeleteRows( " 11 " , myTb);
string expression;
expression = " id>0 " ;
DataRow[] foundRows;
foundRows = myTb.Select(expression, sortOrder);
for ( int i = 0 ; i < foundRows.Length; i ++ )
{
sb.Append(foundRows[i][ " Name " ] + " = " + foundRows[i][ " Timer " ] + " <br/> " );
}
// if (UpdataNews("1", myTb, "xiaohu"))
// {
// sb.Append("update true");
// }
// else
// {
// sb.Append("update False");
// }
sb.Append( " ====================================<br/> " );
myTb = DeleteRows( " 1 " , myTb);
expression = " id>0 " ;
foundRows = myTb.Select(expression, sortOrder);
for ( int i = 0 ; i < foundRows.Length; i ++ )
{
sb.Append(foundRows[i][ " Name " ] + " = " + foundRows[i][ " Timer " ] + " <br/> " );
}
// 用户判断当前的ID是不是存在
if (CheckIDBool( " 1 " , myTb))
{
sb.Append( " Bool true " );
}
else
{
sb.Append( " Bool false " );
}
Response.Write(sb.ToString());
}
/// <summary>
/// 判断是不是存在
/// </summary>
/// <param name="key"></param>
/// <param name="myTb"></param>
/// <returns></returns>
private bool CheckIDBool( string key,DataTable myTb)
{
bool rs = false ;
// string strKey ="";
// strKey = "id=" + key;
// if (myTb.Select(strKey).Length > 0)
// {
// rs = true;
// }
// 方法二:
// northwindDataSet1.Customers.FindByCustomerID("ALFKI");
DataRow foundRow = myTb.Rows.Find(key);
if (foundRow != null )
{
rs = true ;
}
return rs;
}
/// <summary>
/// 根据主键查找DataTable中的值,返回DataRow
/// </summary>
/// <param name="key"></param>
/// <param name="myTb"></param>
/// <returns></returns>
private DataRow[] DtSelect( string key, DataTable myTb)
{
string strKey = "" ;
strKey = " id= " + key;
DataRow[] foundRows;
foundRows = myTb.Select(strKey);
return foundRows;
}
private DataRow[] DtSelect( string key, DataTable myTb, string sortOrder)
{
string strKey = "" ;
strKey = " id= " + key;
DataRow[] foundRows;
foundRows = myTb.Select(strKey, sortOrder);
return foundRows;
}
private bool UpdataNews( string key,DataTable myTb, string name)
{
bool rs = false ;
string strKey = "" ;
strKey = " id=' " + key + " ' " ;
if (CheckIDBool(key, myTb))
{
DataRow[] customerRow =
myTb.Select(strKey);
customerRow[ 0 ][ " Name " ] = name;
customerRow[ 0 ][ " Timer " ] = DateTime.Now.AddYears( - 1 );
rs = true ;
}
return rs;
}
private DataTable DeleteRows( string key, DataTable myTb)
{
DataRow[] foundRows;
foundRows = DtSelect(key,myTb);
if (foundRows.Length > 0 )
{
myTb.Rows.Remove(foundRows[ 0 ]);
}
return myTb;
}
{
DataTable myTb = new DataTable( " auto " );
DataColumn myColid;
myColid = new DataColumn();
myColid.DataType = System.Type.GetType( " System.Int32 " );
myColid.ColumnName = " id " ;
myColid.ReadOnly = true ;
myTb.Columns.Add(myColid);
DataColumn myColname = new DataColumn();
myColname.DataType = System.Type.GetType( " System.String " );
myColname.ColumnName = " Name " ;
myTb.Columns.Add(myColname);
DataColumn myColtimer = new DataColumn();
myColtimer.DataType = System.Type.GetType( " System.DateTime " );
myColtimer.ColumnName = " Timer " ;
myTb.Columns.Add(myColtimer);
/// / 主键的创建
// DataColumn[] PrimaryKeyColumns = new DataColumn[1];
// PrimaryKeyColumns[0] = myTb.Columns["id"];
// myTb.PrimaryKey = PrimaryKeyColumns;
myTb.PrimaryKey = new DataColumn[] { myColid };
DataRow myDr;
myDr = myTb.NewRow();
myDr[ " id " ] = 1 ;
myDr[ " Name " ] = " Name " ;
myDr[ " Timer " ] = DateTime.Now.AddSeconds( 10 );
myTb.Rows.Add(myDr);
myDr = myTb.NewRow();
myDr[ " id " ] = 11 ;
myDr[ " Name " ] = " Name11 " ;
myDr[ " Timer " ] = DateTime.Now.AddSeconds( 8 );
myTb.Rows.Add(myDr);
myDr = myTb.NewRow();
myDr[ " id " ] = 111 ;
myDr[ " Name " ] = " Name111 " ;
myDr[ " Timer " ] = DateTime.Now.AddSeconds( 5 );
myTb.Rows.Add(myDr);
myDr = myTb.NewRow();
myDr[ " id " ] = 2 ;
myDr[ " Name " ] = " Name2 " ;
myDr[ " Timer " ] = DateTime.Now.AddSeconds( 3 );
myTb.Rows.Add(myDr);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string sortOrder = " Timer asc " ;
// foreach (DataColumn dc in myTb.Columns)
// {
// // sb.Append(dc.ColumnName+"<br/>");
// }
// foreach (DataRow dr in myTb.Rows)
// {
// foreach (DataColumn dc in myTb.Columns)
// {
// sb.Append(dr[dc] + "|");
// }
// sb.Append("<br/>");
// }
myTb = DeleteRows( " 11 " , myTb);
string expression;
expression = " id>0 " ;
DataRow[] foundRows;
foundRows = myTb.Select(expression, sortOrder);
for ( int i = 0 ; i < foundRows.Length; i ++ )
{
sb.Append(foundRows[i][ " Name " ] + " = " + foundRows[i][ " Timer " ] + " <br/> " );
}
// if (UpdataNews("1", myTb, "xiaohu"))
// {
// sb.Append("update true");
// }
// else
// {
// sb.Append("update False");
// }
sb.Append( " ====================================<br/> " );
myTb = DeleteRows( " 1 " , myTb);
expression = " id>0 " ;
foundRows = myTb.Select(expression, sortOrder);
for ( int i = 0 ; i < foundRows.Length; i ++ )
{
sb.Append(foundRows[i][ " Name " ] + " = " + foundRows[i][ " Timer " ] + " <br/> " );
}
// 用户判断当前的ID是不是存在
if (CheckIDBool( " 1 " , myTb))
{
sb.Append( " Bool true " );
}
else
{
sb.Append( " Bool false " );
}
Response.Write(sb.ToString());
}
/// <summary>
/// 判断是不是存在
/// </summary>
/// <param name="key"></param>
/// <param name="myTb"></param>
/// <returns></returns>
private bool CheckIDBool( string key,DataTable myTb)
{
bool rs = false ;
// string strKey ="";
// strKey = "id=" + key;
// if (myTb.Select(strKey).Length > 0)
// {
// rs = true;
// }
// 方法二:
// northwindDataSet1.Customers.FindByCustomerID("ALFKI");
DataRow foundRow = myTb.Rows.Find(key);
if (foundRow != null )
{
rs = true ;
}
return rs;
}
/// <summary>
/// 根据主键查找DataTable中的值,返回DataRow
/// </summary>
/// <param name="key"></param>
/// <param name="myTb"></param>
/// <returns></returns>
private DataRow[] DtSelect( string key, DataTable myTb)
{
string strKey = "" ;
strKey = " id= " + key;
DataRow[] foundRows;
foundRows = myTb.Select(strKey);
return foundRows;
}
private DataRow[] DtSelect( string key, DataTable myTb, string sortOrder)
{
string strKey = "" ;
strKey = " id= " + key;
DataRow[] foundRows;
foundRows = myTb.Select(strKey, sortOrder);
return foundRows;
}
private bool UpdataNews( string key,DataTable myTb, string name)
{
bool rs = false ;
string strKey = "" ;
strKey = " id=' " + key + " ' " ;
if (CheckIDBool(key, myTb))
{
DataRow[] customerRow =
myTb.Select(strKey);
customerRow[ 0 ][ " Name " ] = name;
customerRow[ 0 ][ " Timer " ] = DateTime.Now.AddYears( - 1 );
rs = true ;
}
return rs;
}
private DataTable DeleteRows( string key, DataTable myTb)
{
DataRow[] foundRows;
foundRows = DtSelect(key,myTb);
if (foundRows.Length > 0 )
{
myTb.Rows.Remove(foundRows[ 0 ]);
}
return myTb;
}