Presentation is loading. Please wait.

Presentation is loading. Please wait.

DataSource controls in.NET How to access databases.

Similar presentations


Presentation on theme: "DataSource controls in.NET How to access databases."— 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.NET2

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 http://www.oracle.com/technetwork/topics/dotnet/index- 085163.html MySQL – http://www.mysql.com/downloads/connector/net/ http://www.mysql.com/downloads/connector/net/ – http://dev.mysql.com/tech-resources/articles/dotnet/ http://dev.mysql.com/tech-resources/articles/dotnet/ 3Data source controls in.NET

4 Asp:SqlDataSource examles <asp:SqlDataSource ID="SqlDataSource1" runat="server“ SelectCommand="select * from member" ConnectionString="Data Source=(local)\SQLEXPRESS; Initial Catalog=myfirst; Integrated Security=SSPI;"> <asp:SqlDataSource ID="SqlDataSource2“ runat="server" SelectCommand="select * from member" ConnectionString="Data Source=(local)\SQLEXPRESS; User Id=anders; Password=Secret12;"> (local) is sometimes written. (”dot”) 4Data 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 http://www.connectionstrings.com/sql-server-2008 5Data source controls in.NET

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

7 Data Source Mode Property DataSourceMode can have two values – DataSet Read-Write, forward/backwards Default value for the DataSourceMode property Allows filtering, sorting, and paging – DataReader Read-only, forward-only Fastest, most effecient 7Data source controls in.NET

8 Filtering data using SelectParameters <asp:SqlDataSource ID="SqlDataSource3" runat="server" SelectCommand="select * from member where memberID=@memberID“ ConnectionString="Data Source=(local)\SQLEXPRESS; Initial Catalog=myfirst; Integrated Security=SSPI;"> <asp:QueryStringParameter Name="memberID" QueryStringField="memberID" Type="String" /> 8Data 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 Evjen Professional ASP.NET 4 in C# and VB, Wrox/Wiley 2010 – Chapter 7 Data Binding, page 237-253 MSDN AccessDataSource class – http://msdn.microsoft.com/en- us/library/system.web.ui.webcontrols.accessdatasource.aspx http://msdn.microsoft.com/en- us/library/system.web.ui.webcontrols.accessdatasource.aspx MSDN SqlDataSource class – http://msdn.microsoft.com/en- us/library/system.web.ui.webcontrols.sqldatasource.aspx http://msdn.microsoft.com/en- us/library/system.web.ui.webcontrols.sqldatasource.aspx 9Data source controls in.NET


Download ppt "DataSource controls in.NET How to access databases."

Similar presentations


Ads by Google