上一篇 | 下一篇

ADO.NET读书笔记系列

发布: 2008-6-26 22:33 | 作者: admin | 来源: | 查看: 3次

if(rowCustomer == null)

//没有查找客户

else

{

rowCustomer["CompanyName"] ="NewCompanyName";

rowCustomer["ContactName"] ="NewContactName";

}

//推荐使用这种方式

DataRow rowCustomer;

rowCustomer = ds.Tables["Custoemrs"].Rows.Find("ANTON");

if(rowCustomer == null)

//没有查找客户

else

{

rowCustomer.BeginEdit();

rowCustomer["CompanyName"] ="NewCompanyName";

rowCustomer["ContactName"] ="NewContactName";

rowCustomer.EndEdit();

}

//null表示不修改该列的数据

obejct[] aCustomer ={null,"NewCompanyName","NewContactName",null}

DataRow rowCustomer;

rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");

rowCustomer.ItemArray = aCustomer;

③、处理DataRow的空值

//查看是否为空

DataRow rowCustomer;

rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");

if(rowCustomer.IsNull("Phone"))

Console.WriteLine("It's Null");

else

Console.WriteLine("It's not Null");

//赋予空值

rowCustomer["Phone"] = DBNull.Value;

④、删除DataRow

DataRow rowCustomer;

rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");

rowCustomer.Delete();

⑤、清除DataRow

DataRow rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");

rowCustomer.ItemArray = aCustomer;

da.Tables["Customers"].Remove(rowCustomer);

或者

ds.Tables["Customers"].RemoveAt(intIndex);

⑥、使用DataRow.RowState属性 :Unchanged,Detached,Added,Modified,Deleted

private void DemonstrateRowState()

{ // Run a function to create a DataTable with one column. DataTable myTable = MakeTable();DataRow myRow;

// Create a new DataRow. myRow = myTable.NewRow();// Detached row. Console.WriteLine("New Row " + myRow.RowState);

myTable.Rows.Add(myRow);// New row. Console.WriteLine("AddRow " + myRow.RowState);

myTable.AcceptChanges();// Unchanged row. Console.WriteLine("AcceptChanges " + myRow.RowState);

[8]

字号: | 推荐给好友

评分:0

我来说两句