使用DataProvider(Select)
- DataProviders.IDataProvider provider = CreateDataProvider();
- provider.SQL = "SELECT CompanyID as [Identity],Name,ShortName,Code,LegalEntity,Address,PostalCode,Type as CompanyType,CityID,Version " +
- "FROM lt_dictionary.Company WHERE CityID=@cityid";
- provider.AddParameters("@cityid", cityID);
- return provider.ExecuteDataTable();
使用DataProvider(Update)
- DataProviders.IDataProvider provider = CreateDataProvider();
- provider.SQL = "UPDATE lt_dictionary.Company " +
- "SET " +
- "Name=@name, " +
- "ShortName=@shortName," +
- "Code=@code," +
- "LegalEntity=@legalEntity," +
- "Address=@address," +
- "PostalCode=@postalCode, " +
- "Type=@type," +
- "CityID=@cityID " +
- "WHERE CompanyID=@id AND Version=@ver";
- provider.AddParameters("@name", company.Name);
- provider.AddParameters("@shortName", company.ShortName);
- provider.AddParameters("@Code", company.Code);
- provider.AddParameters("@LegalEntity", company.LegalEntity);
- provider.AddParameters("@address", company.Address);
- provider.AddParameters("@postalCode", company.PostalCode);
- provider.AddParameters("@type", company.CompanyType.ToString());
- provider.AddParameters("@cityID", company.City.Identity);
- provider.AddParameters("@id", original_company.Identity);
- provider.AddParameters("@ver", original_company.Version, DataProviders.ByteArrayFamily.Timestamp);
- return provider.ExecuteNonQuery() > 0;
使用DataProvider(Insert)
- DataProviders.IDataProvider provider = CreateDataProvider();
- provider.SQL = "INSERT INTO lt_dictionary.City " +
- "([Name],PostalCode,DistanceCode,Province,Longitude,Latitude)" +
- "VALUES " +
- "(@Name,@PostalCode,@DistanceCode,@Province,@Longitude,@Latitude)";
- provider.AddParameters("@name", city.Name);
- provider.AddParameters("@postalCode", city.PostalCode);
- provider.AddParameters("@distanceCode", city.DistanceCode);
- provider.AddParameters("@province", city.Province);
- provider.AddParameters("@longitude", city.Longitude);
- provider.AddParameters("@latitude", city.Latitude);
- return provider.ExecuteNonQuery() > 0;
通过上述的代码,可以发现,使用了我们的DataProvider后,程序员对ADO.Net的了解被降到最低程度,其只要关心具体的SQL指令和参数的赋值,其他内容不再需要其关注。很高程度的提高了程序员的开发效率。