Operation of the database objects can be encoded to achieve, of course, .NET provides a more convenient visual mechanisms can help us save a lot of trouble.
First, an increase in the project new item, select Dataset dataset, as shown:
CustomersXSD.xsd example, a new set of data will be NorthWind database Customers table onto the panel current design, as shown below:
, Manually set to increase as shown in FIG Adapter automatically generated object named CustomersTableAdapter FIG three methods,
Wherein the configuration shown in FIG. GetByPrimaryKey method:
The following code demonstrates how to use CustomerXSD, CustomersTableAdapter to achieve the data sheet Customers add a record:
First, assume the code has been generated table object class Customer CustomersModel, code is as follows: Note and red portions
using Model; // database entity class Model
using CustomersXSDTableAdapters; // namespace introduced Adapter
public class CustomersDAL
{
private CustomersTableAdapter _CustomersTableAdapter = null;
protected CustomersTableAdapter Adapter
{
get
{
if (_CustomersTableAdapter == null)
_CustomersTableAdapter = new CustomersTableAdapter();
return _CustomersTableAdapter;
}
}
// Get method, all the data in the table obtained
public DataTable Get()
{
return Adapter.GetData();
}
// 根据主键得到数据表
public DataTable GetByPrimaryKey(String CustomerID)
{
return Adapter.GetByPrimaryKey(CustomerID);
}
//增加一条记录
public bool Add(CustomersModel model)
{
CustomersXSD.CustomersDataTable TargetTable = new CustomersXSD.CustomersDataTable();
CustomersXSD.CustomersRow TargetRow = TargetTable.NewCustomersRow();
TargetRow.CustomerID = model.CustomerID;
TargetRow.CompanyName = model.CompanyName;
TargetRow.ContactName = model.ContactName;
TargetRow.ContactTitle = model.ContactTitle;
TargetRow.Address = model.Address;
TargetRow.City = model.City;
TargetRow.Region = model.Region;
TargetRow.PostalCode = model.PostalCode;
TargetRow.Country = model.Country;
TargetRow.Phone = model.Phone;
TargetRow.Fax = model.Fax;
TargetTable.AddCustomersRow(TargetRow);
int rowaffected = Adapter.Update(TargetTable);
return rowaffected == 1;
}
//更新一条记录
public bool Update(CustomersModel model)
{
CustomersXSD.CustomersDataTable TargetTable = Adapter.GetByPrimaryKey(model.CustomerID);
if (TargetTable.Count == 0)
return false;
CustomersXSD.CustomersRow TargetRow = TargetTable[0];
TargetRow.CustomerID = model.CustomerID;
TargetRow.CompanyName = model.CompanyName;
TargetRow.ContactName = model.ContactName;
TargetRow.ContactTitle = model.ContactTitle;
TargetRow.Address = model.Address;
TargetRow.City = model.City;
TargetRow.Region = model.Region;
TargetRow.PostalCode = model.PostalCode;
TargetRow.Country = model.Country;
TargetRow.Phone = model.Phone;
TargetRow.Fax = model.Fax;
int rowsaffected = Adapter.Update(TargetRow);
return rowsaffected == 1;
}
// delete a record
public bool Delete(String CustomerID)
{
int rowsaffected = Adapter.DeleteByCustomerID(CustomerID);
return rowsaffected == 1;
}
}
Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2012/11/28/2792636.html