转载请注明出处:http://surfsky.cnblogs.com/
---------------------------------------------------------
-- DataSource 支持的数据源
-- DataTable
-- IList
-- BindingList
-- XXXDataSource
---------------------------------------------------------
DataTable
grid.DataSource = dt;
grid.DataBind();
IList
int articleId = Convert.ToInt32(Request.QueryString["articleId"]);
IList<BlogArticleImage> images = BlogArticleImage.ListArticleImages(articleId);
this.gvImages.KeyFieldName = "ImageId";
this.gvImages.DataSource = images;
this.gvImages.DataBind();
BindingList
private void CreateQuotes()
{
BindingList<Quote> res = new BindingList<Quote>();
foreach(string name in names)
{
Quote q = new Quote(name);
q.Value = (decimal)GetRandom().Next(800, 2000) / (decimal)10;
res.Add(q);
}
Session["Quotes"] = res;
}
AccessDataSource
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb"
SelectCommand="SELECT * FROM [Customers]"
DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = ?"
InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
UpdateCommand="UPDATE [Customers] SET [CompanyName] = ?, [ContactName] = ?, [ContactTitle] = ?, [Address] = ?, [City] = ?, [Region] = ?, [PostalCode] = ?, [Country] = ?, [Phone] = ?, [Fax] = ? WHERE [CustomerID] = ?"
/>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb"
OnDeleting="AccessDataSource1_Modifying" OnInserting="AccessDataSource1_Modifying" OnUpdating="AccessDataSource1_Modifying"
SelectCommand="SELECT * FROM [Customers]" DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = ?"
InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
UpdateCommand="UPDATE [Customers] SET [CompanyName] = ?, [ContactName] = ?, [City] = ?, [Region] = ?, [Country] = ? WHERE [CustomerID] = ?">
<DeleteParameters>
<asp:Parameter Name="CustomerID" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="ContactName" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Region" Type="String" />
<asp:Parameter Name="Country" Type="String" />
</UpdateParameters>
</asp:AccessDataSource>
protected void AccessDataSource1_Modifying(object sender, SqlDataSourceCommandEventArgs e) {
DemoSettings.AssertNotReadOnly();
}
用代码实现
AccessDataSource ds = new AccessDataSource();
ds.DataFile = AccessDataSource1.DataFile;
ds.SelectCommand = "select Photo from [Employees] where employeeid=" + id;
DataView view = (DataView)ds.Select(DataSourceSelectArguments.Empty);
if(view.Count > 0) return view[0][0] as byte[];
return null;
ObjectDataSource
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
TypeName="Quotes"
SelectMethod="LoadQuotes"
/>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="PersonRegistration"
TypeName="MyPersonProvider"
SelectMethod="GetList" UpdateMethod="Update" InsertMethod="Insert"
/>
<asp:objectDataSource id="ObjectDataSource1" runat="server"
typename="PersonManager"
selectMethod="SelectPersons"
deleteMethod="DeletePerson"
updateMethod="UpdatePerson"
insertMethod="InsertPerson" >
<insertParameters>
<asp:parameter name="Id" type="Int32" />
</insertParameters>
</asp:objectDataSource>
ObjectDataSource.Parameters
<SelectParameters>
<asp:SessionParameter Name="IGYSID" SessionField="ID" Type="Int32" />
<asp:SessionParameter DefaultValue="0" Name="ICGFS" SessionField="ICGFS" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="IWZID" />
<asp:Parameter Name="IGYSID" />
<asp:Parameter Name="ICGMXID" />
<asp:Parameter Name="IGYSBJID" />
<asp:Parameter Name="NBJ" />
<asp:Parameter Name="CBZ" />
</UpdateParameters>
<UpdateParameters>
<asp:FormParameter FormField="makeid" Name="MakeID" Type="String" />
<asp:FormParameter FormField="name" Name="Name" Type="String" />
<asp:FormParameter FormField="id" Name="ID" Type="String" ConvertEmptyStringToNull="False" />
</UpdateParameters>
ObjectDataSource 使用的类
(以下代码整理并修改至《ASP.NET 2.0 Revealed》P71-P78)
(另外一个购物篮的例子,参考P138)
public class Person
{
private int id;
private string firstName;
private string lastName;
public int Id {...}
public string FirstName {...}
public string LastName {...}
public Person(int id, string firstName, string lastName)
{
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
}
public class PersonCollection : List<Person> {}
public class PersonManager
{
private const string personsKey = "persons";
public PersonCollection SelectPersons()
{
HttpContext context = HttpContext.Current;
if (context.Application[personKey] == null)
{
PersonCollection persons = new PersonCollection();
persons.Add(new Person(0, "Patrick", "Lorenz"));
persons.Add(new Person(0, "Patrick", "Lorenz"));
persons.Add(new Person(0, "Patrick", "Lorenz"));
persons.Add(new Person(0, "Patrick", "Lorenz"));
context.Application[personKey] = persons;
}
return context.Application[personKey] as PersonCollection;
}
public Person SelectPerson(int id)
{
foreach (Person p in SelectPersons())
if (p.Id == id)
return p;
return null;
}
public void DeletePerson(int id)
{
PersonCollection persons = (Application[personKey] as PersonCollections);
Person person = SelectPerson(id);
if (person != null)
persons.Remove(person);
}
public void InsertPerson(int id, string firstName, string lastName)
{
PersonCollection persons = (Application[personKey] as PersonCollections);
persons.Add(new Person(id, firstName, lastName));
}
public void UpdatePerson(int id, string firstName, string lastName)
{
Person person = SelectPerson(id);
if (person != null)
{
person.FirstName = firstName;
person.LastName = lastName;
}
}
}