If you are using either Microsoft Visual Studio or Microsoft Visual Web Developer to create your web site, the SQLDataSource class is represented in the Toolbox by a SQLDataSource button in the Data section. Therefore, to create a data source, drag this object and drop it on your form. To configure it, you can click the arrow button on its right side and click Con
This would open the Configure Data Source wizard:
The SQL data source can be used on various types of databases, including Microsoft SQL Server, Microsoft Access, or Oracle, etc. In the first page of the wizard, click the New Connection button. This would open the Add Connection dialog box:
Make sure the Data Source text box displays Microsoft SQL Server (SqlClient) string. You can also click the Change button. In the Choose Data Source dialog box, click Microsoft SQL Server and click OK. This would display the above Add Connection dialog box. In the Server Name combo box, select your server. If you know its name, you can type it. If you are using a database in the current computer, you can type (local). Specify the type of authentication you will use: Windows Authentication or SQL Server Authentication. In the Select or Enter a Database Name combo box, select the desired database, click Test Connection, and click OK. After creating the connection, you would get back to the first page of the wizard:
You would click next.
The SQLDataSource is used as a control. Therefore, to manually initiate it, create an <asp:SQLDataSource> tag in your form. Like every control, the tag should have an ID and a runat attribute. Here is an example: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <form id="frmExercise" runat="server"> <asp:SQLDataSource id="sdsExercise" runat="server"></asp:SQLDataSource> </form> </body> </html>
A web configuration file is a document that specifies how to use some details of a web site. If you use Microsoft Visual Studio or Microsoft Visual Web Developer to create your web site, a web configuration file would be generated for you. A web configuration document is created like an XML file. As an XML-like file, it starts with <?xml version="1.0"?>. The root of the document is the <configuration> tag. Here is an example of starting a web configuration file: <?xml version="1.0"?> <configuration> </configuration> After creating the file, you must save it in the root directory (or folder) of your project. Save the file with the name web.config. One of the roles of a web configuration file is to provide a connection string. This specifies how a database would be accessed. A connection string is a value made of different sections that each uses the formula key=value. To create the connection string, in your web configuration file, include a <connectionStrings> tag: <?xml version="1.0"?> <configuration> <connectionStrings></connectionStrings> </configuration> As you can see from its name, the <connectionStrings> tag holds a collection of values. When creating a connection string, you must give it a name so you can refer to it later. If you are using the Configuration Data Source dialog box, in the second page of the wizard, enter the name of the connection string:
After specifying the name, click Next. If you are manually creating the configuration file, to specify the name, create an <add> tag equipped with an attribute named name. Assign the desired name to the attribute. Here is an example: <?xml version="1.0"?> <configuration> <connectionStrings> <add name="strExercise"></add> </connectionStrings> </configuration> Besides the name, you must add an attribute named connectionstring to the <add> tag. Assign the values of the connection string to this attribute. Here is an example for a Microsoft SQL Server database: <?xml version="1.0"?> <configuration> <connectionStrings> <add name="strExercise" connectionString="Data Source=(local);Initial Catalog=Exercise;Integrated Security=True"></add> </connectionStrings> </configuration> Besides the name and the connection string, you must also specify the data provider. To support this, the <add> attribute is equipped with an attribute named providerName. For a Microsoft SQL Server database, use providerName="System.Data.SqlClient". This would be done as follows: <?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="strExercise"
connectionString="Data Source=(local);Initial Catalog=SolasPropertyRental1;Integrated Security=True"
providerName="System.Data.SqlClient">
</add>
</connectionStrings>
</configuration>
A web configuration file can contain more information than this. |
|
|||||||||||||||||
|
|
||||||||||||||||||
To perform its duties, the primary requirement of a data source is to establish a connection to a database. To support this, the SQLDataSource class is equipped with a property named ConnectionString. To create a connection string, add an attribute named ConnectionString to the <asp:SQLDataSource> tag: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <form id="frmExercise" runat="server"> <asp:SQLDataSource id="sdsExercise" ConnectionString="" runat="server"> </asp:SQLDataSource> </form> </body> </html> |
|
|
The value of the connection string attribute is to specify the connection string of the web configuration file. To specify this in the ConnectionString attribute, start the string with <%$ and end it with %>. This would be done as follows: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <form id="frmExercise" runat="server"> <asp:SQLDataSource id="sdsExercise" ConnectionString="<%$ %>" runat="server"> </asp:SQLDataSource> </form> </body> </html> Inside of this delimiter, type <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <form id="frmExercise" runat="server"> <asp:SQLDataSource id="sdsExercise" ConnectionString="<%$ ConnectionStrings:strExercise %>" runat="server"> </asp:SQLDataSource> </form> </body> </html>
A data source must specify the type of operation that would be performed on its connection. Operations include creating records, selecting values from a database, or deleting records. To support this, the SQLDataSource class is equipped with various properties. One of them is called SelectCommand. This specifies a list of records that would be retrieved from a database. If you are visually creating the data source, in the third page of the Configure Data Source wizard, you will specify the command. To assist you with commands, in the <asp:SQLDataSource> tag, add an attribute that holds the name of the command and assign the appropriate string to it. |
|