8.
() , ( ).
, , . , . .
(Data Provider) , . , .
ADO.NET , , :
SQL Managed Provider (SQL Server.NET Data Provider) - Microsoft SQL Server 7.0 . , TabularData Stream (TDS) ADO, ODBC, - . MS SQL Server, .
ADO Managed Provider (OleDb.NET Data Provider) - . . SQL Server Provider, SQL Server .
.
Connection
- Connection () . Visual Studio.NET :
SQLConnection ( SQL Server 7.0 ),
OleDbConnection ( ).
Connection ( , ) ConnectionString, . , , .
Connection :
,
.
.
- OleDbConnection ( SQLConnection) .
. ConnectionString , .
, OleDbConnection
|
|
.
ConnectionString | string | , . |
ConnectionTimeout | Int32 | , ( ). |
Container | string | Gets the IContainer that contains the Component. |
Database | string | Gets , . |
DataSource | string | Gets the server name or file name of the data source. , . (SQL Server, Oracle) , . (Access) . |
Provider | string | Gets OLE DB , "Provider= " clause . |
ServerVersion | string | Gets a string containing the version of the server to which the client is connected. |
Site | string | Gets or sets the ISite of the Component. |
State | string | Gets . |
ConnestionState. .
Value | ||
Broken | The connection to the data source is broken. , . A connection in this state may be closed and then re-opened. (This value is reserved for future versions of the product.) | |
Closed | . | |
Connecting | The connection object is connecting to the data source. (This value is reserved for future versions of the product.) | |
Executing | The connection object . (This value is reserved for future versions of the product.) | |
Fetching | . (This value is reserved for future versions of the product.) | |
Open | . |
BeginTransaction | . . |
ChangeDatabase | OleDbConnection. |
Close | . . |
CreateCommand | OleDbCommand, OleDbConnection. |
CreateObjRef ( MarshalByRefObject) | , -, . |
Dispose ( Component) | . , Component. |
EnlistDistributedTransaction | . |
Equals ( Object) | . , Object. |
GetHashCode ( Object) | - , , -. |
GetLifetimeService ( MarshalByRefObject) | , . |
GetOleDbSchemaTable | , GUID, . |
GetType ( Object) | Type . |
InitializeLifetimeService ( MarshalByRefObject) | , . |
Open | , ConnectionString. |
ReleaseObjectPool | . , OleDbConnection , . |
ToString ( Object) | String, Object. |
|
|
Dispose | . . , OleDbConnection. |
Finalize ( Component) | . , , Component, . C# C++ . |
GetService ( Component) | , , Component Container. |
MemberwiseClone ( Object) | Object. |
Disposed | Adds an event handler to listen to the Disposed event on the component. |
InfoMessage | (SQL Server) . occurs when the provider sends a warning or an informational message. |
StateChange | Occurs when the state of the connection changes. |
Command
, . , :
, ,
INSERT, UPDATE, DELETE, ,
, ,
DataBase Definition Language (DDL), CREATE TABLE,
DataAdapter, DataSet,
DataReader,
SqlCommand XML,
, .
Command , :
DML (Data Manipulation Langauge) , (INSERT, UPDATE, DELETE),
DDL (Data Definition Language) , (CREATE)
, DataReader (SELECT).
SqlCommand OleDbCommand. . :
,
SQL,
, .
Command ( ) :
ExecuteNonQuery , , , INSERT, UPDATE, DELETE,
ExecuteScalar , ,
ExecuteReader DataReader.
ADO.NET Data Provider :
- Connection .
Command DataAdapter.
Command .
|
|
, Command DataReader.
, , DataSet, DataAdapter.
Command DataAdapter.
, , , ADO.NET :
Connection Object ,
Dataset Object ,
Command Object .
Command:
( Connection),
CreateCommand Connection.
DataReader
, DataReader.
, .
. Item, ( , !).
GetOrdinal - DataReader , .
. DataReader .
DataReader ExecuteReader Command (SqlCommand.ExecuteReader SqlDataReader, OleDbCommand.ExecuteReader OleDbDataReader).
, (, ), - DataReader.
ExecuteReader - DataReader. .
, - DataReader, .
DataReader . . DataReader . ! .
- (Data Source Controls)
ADO.NET, , . IDataSource, . . ADO.NET. ADO , - DataBind. :
<asp:BulletedList ID="BulletedList1" runat="server" BulletStyle="Square" DataTextField="CategoryName" DataValueField="CategoryID"> </asp:BulletedList> protected void Page_Load(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(@"Data Source=(local)\sqlexpress;Initial Catalog=Northwind;Integrated Security=True"); SqlCommand cmd = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories", conn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); BulletedList1.DataSource = ds; BulletedList1.DataBind(); }c - DataSourceID. . .
|
|
ASP.NET 5 - : SqlDataSource, AccessDataSource ObjectDataSource XmlDataSource SiteMapDataSource .
SqlDataSource . Sql , , Sql, MS SQL Server.
AccessDataSource Access. ,
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/guestbook.mdb" SelectCommand="SELECT [WriteDate], [UserName], [UserMail], [Message] FROM [guestbook]"> </asp:AccessDataSource>SiteMapDataSource XmlDataSource, .
ObjectDataSource -.
, , . . .
SqlDataSource
SqlDataSource SqlConnection SqlDataAdapter( ).
, web.config.
<add name="DemoBaseConnectionString1" connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=DemoBase;Integrated Security=True" providerName="System.Data.SqlClient" />ConnectionString .
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DemoBaseConnectionString1 %>" ProviderName="<%$ ConnectionStrings:DemoBaseConnectionString1.ProviderName %>" </asp:SqlDataSource>DataSourceMode SqlDataSource , DataReader DataSet . DataReader .
, SqlDataAdapter: SelectCommand, SelectCommandType, DeleteCommand, DeleteCommandType . SelectCommandType 2 Text StoredProcedure. , .
Select DataSourceSelectArguments DataSet IDataReader DataSourceMode, .
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT * FROM [Customers]" ProviderName="<%$ ConnectionStrings:NorthwindConnectionString.ProviderName %>"> </asp:SqlDataSource>SqlDataSource Customers DataSet.
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="[Ten Most Expensive Products]" DeleteCommandType="StoredProcedure"> </asp:SqlDataSource>Select . , SqlDataSource .
. . .
.
Parameter: CookieParameter cookie, FormParameter - , QuerystringParameter , ProfileParameter SessionParameter - .
<asp:Parameter Name="UID" Type="Int32" DefaultValue="0" />, SqlDataSource GridView, FormView, DetailsView. .
|
|
ControlParameter, . , . Text, .
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="Sales by Year" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:Parameter Name="Beginning_Date" Type="DateTime" DefaultValue="01.01.1998"/> <asp:ControlParameter Name="Ending_Date" Type="DateTime" ControlID="Calendar2"/> </SelectParameters> </asp:SqlDataSource>. ConvertEmptyToNull Null, ( System.String.Empty).
CancelSelectOnNullParameter , , - Null.
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT * FROM [Customers] where @Country is null or Country = @Country" ProviderName="<%$ ConnectionStrings:NorthwindConnectionString.ProviderName %>" CancelSelectOnNullParameter="False"> <SelectParameters> <asp:QueryStringParameter Name="Country" QueryStringField="Country" /> </SelectParameters> </asp:SqlDataSource>,
http://localhost:3457/WebSite4/CustomersByCountry.aspx?Country=UK
. SqlDataSource , , . , , , . , , .
, .
SqlDataSource DataSet. DataSourceMode DataReader, EnableCaching True, NonSupportedException.
CacheDuration, Infinite, .
CacheDuration CacheExpirationPolicy. CacheExpirationPolicy Absolute, , CacheDuration, . CacheExpirationPolicy Sliding, SqlDataSource . , CacheDuration Select-.
FilterExpression , , {0}, {1}, , FilterParameters.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT * FROM [Customers]" ProviderName="<%$ ConnectionStrings:NorthwindConnectionString.ProviderName %>" EnableCaching="True" CacheExpirationPolicy="Sliding"> </asp:SqlDataSource>, GridView.
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT * FROM [Customers]" ProviderName="<%$ ConnectionStrings:NorthwindConnectionString.ProviderName %>" FilterExpression="CustomerID={0} " <FilterParameters> <asp:ControlParameter Name="CustomerID" ControlId="GridView1" PropertyName=SelectedValue"></asp:ControlParameter> </FilterParameters> </asp:SqlDataSource>, DetailsView. DetailsView , , GridView1.
SortParameterName , , Desc . Select, .
ObjectDataSource
, -. ? , , . - .NET. , App_Code. ObjectDataSource - , . . - , , . ObjectDataSource , SqlDataSource, , , .
TypeName ObjectDataSource . - 4 ( ) , , . ObjectDataSource .
, SelectMethod -, .
- , . - , . , , , , IEnumerable. UpdateMethod , . DeleteMethod InsertMethod.
- SelectCount, .ObjectDataSource , .
:
public class Continent { ArrayList ContinentArrayList; public Continent() { ContinentArrayList = new ArrayList(); ContinentArrayList.Add("Worldwide"); ContinentArrayList.Add("America"); ContinentArrayList.Add("Africa"); ContinentArrayList. Add("Asia-Pacific"); } public ArrayList List() { return ContinentArrayList; } public int SelectCount() { return ContinentArrayList.Length; } }ObjectDataSource, ArrayList IEnumerable. *Command ObjectDataSource *Method.
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="List" TypeName=" Continent "> </asp:ObjectDataSource> <asp:RadioButtonList ID="RadioButtonList1" runat="server" DataSourceID="ObjectDataSource1"> </asp:RadioButtonList></div>, , , Continent. , . - , XML- -. - , . Windows Forms.
ObjectDataSource , . Customers. App_Code dataset. Customers. . NorthWindConnectionString ( , , ). : SQL, . , , , Northwind . , QueryBuilder, MS Access. Customers, .
SELECT CustomerID, CompanyName, ContactName, ContactTitle, Country, City FROM Customers
QueryBuilder AdvancedOptions.
, . , . , . .
Customers.xsd, XML(XML Schema Definition, , , Customers.xss. .
. ObjectDataSource. SmartTag . CustomersDataAdapters.CustomersDataAdapter. Select, Update, Delete, Insert. Finish. ObjectDataSource , GridView.
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData" TypeName="CustomersTableAdapters.CustomersTableAdapter" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" UpdateMethod="Update"> <DeleteParameters> <asp:Parameter Name="Original_CustomerID" Type="String" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="CustomerID" Type="String" /> <asp:Parameter Name="CompanyName" Type="String" /> <asp:Parameter Name="ContactName" Type="String" /> <asp:Parameter Name="ContactTitle" Type="String" /> <asp:Parameter Name="Country" Type="String" /> <asp:Parameter Name="City" Type="String" /> <asp:Parameter Name="Original_CustomerID" Type="String" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="CustomerID" Type="String" /> <asp:Parameter Name="CompanyName" Type="String" /> <asp:Parameter Name="ContactName" Type="String" /> <asp:Parameter Name="ContactTitle" Type="String" /> <asp:Parameter Name="Country" Type="String" /> <asp:Parameter Name="City" Type="String" /> </InsertParameters> </asp:ObjectDataSource> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="ObjectDataSource1"> <Columns> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" /> <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" /> <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" /> <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" /> <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" /> <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" /> <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" /> </Columns> </asp:GridView>- . .xsd v C# xsd.exe.
xsd.exe /dataset /language:CS Customers.xsd.ObjectDataSource. Personal Starter Kit PhotoManager, Personal.mdf.
public static Stream GetPhoto(int photoid, PhotoSize size) { using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString)) { using (SqlCommand command = new SqlCommand("GetPhoto", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@PhotoID", photoid)); command.Parameters.Add(new SqlParameter("@Size", (int)size)); bool filter =!(HttpContext.Current.User.IsInRole("Friends") || HttpContext.Current.User.IsInRole("Administrators")); command.Parameters.Add(new SqlParameter("@IsPublic", filter)); connection.Open(); object result = command.ExecuteScalar(); try { return new MemoryStream((byte[])result); } catch { return null; } } }, , , .