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 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
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 – html html MySQL – – 3Data source controls in.NET
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
Connection string Information needed to connect to the database – DataSource=ServerName\ServerInstance Server name can be –. – (local) – Localhost » 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 – 5Data source controls in.NET
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
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
Filtering data using SelectParameters <asp:SqlDataSource ID="SqlDataSource3" runat="server" SelectCommand="select * from member where ConnectionString="Data Source=(local)\SQLEXPRESS; Initial Catalog=myfirst; Integrated Security=SSPI;"> <asp:QueryStringParameter Name="memberID" QueryStringField="memberID" Type="String" /> 8Data source controls in.NET
References George Shepherd ASP.NET 4 Step by Step, Microsoft Press 2010 – Chapter 10 Data Binding, page Imar Spaanjaars Beginning ASP.NET 4 in C# and VB, Wrox/Wiley 2010 – Appendix B Configuring SQL Server 2008, page Bill Evjen Professional ASP.NET 4 in C# and VB, Wrox/Wiley 2010 – Chapter 7 Data Binding, page MSDN AccessDataSource class – us/library/system.web.ui.webcontrols.accessdatasource.aspx us/library/system.web.ui.webcontrols.accessdatasource.aspx MSDN SqlDataSource class – us/library/system.web.ui.webcontrols.sqldatasource.aspx us/library/system.web.ui.webcontrols.sqldatasource.aspx 9Data source controls in.NET