1 / 9

DataSource controls in .NET

DataSource controls in .NET. How to access databases. Where is that database?. The physical database might be in many places On another server IP address + port number required On the same server, as the ASP.NET application

ehren
Télécharger la présentation

DataSource controls in .NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DataSource controls in .NET How to access databases

  2. Where is that database? • The physical database might be in many places • On another server • IP address + port number required • On the same server, as the ASP.NET application • If you work with a Microsoft SQL Server database put the database file (somename.mdf) in an APP_DATA folder in your ASP.NET project Data source controls in .NET

  3. DataSource Controls • A DataSource control has access to a database • AccessDataSource control • Access to a Microsoft Access Database • SqlDataSource control • Access to a Microsoft SQL Server database • Oracle • http://www.oracle.com/technetwork/topics/dotnet/index-085163.html • MySQL • http://www.mysql.com/downloads/connector/net/ • http://dev.mysql.com/tech-resources/articles/dotnet/ Data source controls in .NET

  4. Asp:SqlDataSourceexamles <asp:SqlDataSource ID="SqlDataSource1" runat="server“ SelectCommand="select * from member" ConnectionString="Data Source=(local)\SQLEXPRESS; Initial Catalog=myfirst; Integrated Security=SSPI;"> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2“ runat="server" SelectCommand="select * from member" ConnectionString="Data Source=(local)\SQLEXPRESS; UserId=anders; Password=Secret12;"> </asp:SqlDataSource> (local) is sometimeswritten . (”dot”) Data source controls in .NET

  5. Connection string • Information needed to connect to the database • DataSource=ServerName\ServerInstance • Server name can be • . • (local) • Localhost • 127.0.0.1 didn’t work for me • The name of the server machine • Server instance • Necessary if you have more than one instance of SQL Server on the server machine • Example: SQLEXPRESS • Catalog=myfirst • The name of the database to connect to • Security • Integrated Security: Use Windows authentication • Provide Username/password • More information • http://www.connectionstrings.com/sql-server-2008 Data source controls in .NET

  6. Connectionstring in web.config <configuration> <connectionStrings> … <add name="myfirstConnectionString" connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=myfirst; Integrated Security=SSPI;" providerName="System.Data.SqlClient" /> </connectionStrings> <asp:SqlDataSource ID="SqlDataSource4" runat="server" SelectCommand="SELECT * FROM [member]" ConnectionString="<%$ ConnectionStrings:myfirstConnectionString%>"> </asp:SqlDataSource> Data source controls in .NET

  7. Data Source Mode Property • <asp:SqlDataSource… DataSourceMode=… > • DataSourceModecan have twovalues • DataSet • Read-Write, forward/backwards • Default value for the DataSourceModeproperty • Allowsfiltering, sorting, and paging • DataReader • Read-only, forward-only • Fastest, most effecient Data source controls in .NET

  8. Filtering data usingSelectParameters <asp:SqlDataSource ID="SqlDataSource3" runat="server" SelectCommand="select * from member where memberID=@memberID“ ConnectionString="Data Source=(local)\SQLEXPRESS; Initial Catalog=myfirst; Integrated Security=SSPI;"> <SelectParameters> <asp:QueryStringParameterName="memberID" QueryStringField="memberID" Type="String" /> </SelectParameters> </asp:SqlDataSource> Data source controls in .NET

  9. References • George Shepherd ASP.NET 4 Step by Step, Microsoft Press 2010 • Chapter 10 Data Binding, page 221-226 • Imar Spaanjaars Beginning ASP.NET 4 in C# and VB, Wrox/Wiley 2010 • Appendix B Configuring SQL Server 2008, page 757-775 • Bill EvjenProfessional ASP.NET 4 in C# and VB, Wrox/Wiley 2010 • Chapter 7 Data Binding, page 237-253 • MSDN AccessDataSourceclass • http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.accessdatasource.aspx • MSDN SqlDataSourceclass • http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.aspx Data source controls in .NET

More Related