Because Access does not really serve high-performance applications, a site that wants to have multiple simultaneous users must deploy a data source that is more appropriate than access. This article describes how to get data from Microsoft SQL Server (an enterprise-class RDMS).
The SQL Server full edition includes three sections. The first is the engine, which is used to actually organize the data and read and write to the command response. The second is a developer's tool package for manipulating the database, such as Query Analyzer and Data Transformation Services. The last is a tool for managing data, including backup programs and replication modes.
While the full version has immeasurable benefits for large businesses, many developers do not need a complete set of tools. Fortunately, Microsoft offers a free version of the SQL Server engine, called SQL Server Express (SSE). Although it has a command-line tool (Osql.exe) that uses T-SQL commands to import patterns and data, it does not contain rich graphical tools that are available in the full version of SQL Server. However, you can easily use the SSE development database using Visual Studio or Visual Web developer. SSE has a limitation of handling only local connections (it is not possible to run SSE on a different machine than the Web server). SSE is a great choice for people with certain hobbies and for students ' web sites. You may use MSDE based on SQL Server 2000, and SSE is based on the Yukon version of SQL Server.
Unless otherwise specifically stated, all of the techniques described here can be applied to three forms of SQL Server (full edition products, SSE, and MSDE), so the common term SQL Server contains these three forms.
Because SSE is just an engine and does not have a built-in development tool (up to 2004), you must use other tools to create, modify, or populate the database. The main tool we will use for this is Visual Studio or Visual Web Developer, which provides database Explorer as part of its interface. Note that the database exporer can change the data and schema in the local database, but it can only change the data (not the structure) in the remote database. In Visual Studio, Database Explorer is called Server Explorer because it includes additional functionality to handle non-database servers. We will also begin by simply using the Osql.exe command-line tool at the beginning of this book to import the initial database that will be used throughout this book. The third way to modify the database structure is to open access and then link an external table in the SSE database.
Using SQL Server requires familiarity with some vocabulary. SQL Server is installed on a machine called server, and can be referenced by its machine name. If SQL Server is on the same machine as the requesting software, the machine can be referenced by (local). The engine can be installed on a single machine multiple times, and each installation is called an instance (instance). SSE installs an instance named (local) \SQLExpress. Within an instance, you can create a database. The database has a table with fields (field) and records (record). A database also has a set of tables, fields, and constraints for use by data consumers, called views. Stored procedures (stored procedures,sproc) can perform tasks on data. An instance of SQL Server automatically installs an account with user name SA, which has permission to perform all actions on all objects when using SQL Server authentication for the user. When using Windows Authentication (also known as hybrid authentication), users who log into Windows also log in to SSE.
preparation before using SQL Server database
Taking the time to study the database will reduce the error when you design pages that use data. Before you write a page that uses SQL Server, check to see if you have the following types of information available:
· Server, instance, and database name--confirm the exact spelling of the server name, instance, and database name. If you have only one instance of a full version of SQL Server on the server, you do not need to use the instance name. However, even if there is only one instance of SSE on the server, it must be explicitly referenced with myserver\sqlexpress. Make it clear whether you want to test the actual data or the deployment backup of the test database.
· Security information-you need to know your user ID and password to authenticate access to the development database. Similarly, please check whether SSE is using Windows Authentication or SQL authentication (the installation in Chapter 1th has stated that SSE should use Windows authentication).
· Database schema--Understanding the schema of the database. Gets the exact spelling of the table and field names, automatically generated or locked fields, dependencies, and constraints. Carefully note the underscores and spaces in the object name. Find out from the database administrator whether you are using a table directly or using a view or stored procedure (SPROC). Using the latter will probably require parameters for special data types. The SQL syntax used to check these metadata is listed later in this chapter.
· Test the SQL statement (optional)-You may suspect the syntax or logic of the SQL statement. You will find that the effective way to test statements is to use the development tools of Query Analyzer such as SQL, rather than testing them for the first time they appear on the ASPX page.
Connection String
The primary difference between the syntax of a AccessDataSource control and a SqlDataSource control is that the database is used in different ways. For the MDB, we simply provide the filename and pathname, while SqlDataSource uses the connection string name that contains the server name, database name, and login information. The syntax of the connection string differs from the syntax we are familiar with in Visual Basic or C #, and is why many students make mistakes. A typical connection string is as follows:
Connectionstring= "Server=myserver; Database=mydatabase; User Id=myid; Password=mypass "
The format of multiple-line connection strings can improve readability, as follows:
Connectionstring= "
Server=myserver;
Database=mydatabase;
User ID = MyID;
Password=mypass "
First, we'll talk about grammar. The entire string in the source code is in double quotes. When you specify a connection string in the vWD property window, you do not need quotes, and VWD will be added automatically. In the middle of quotation marks are pairs of formats such as Criteria=value. The semicolon separates the information. Note that you do not need to use quotes for values. Also, note that even if a sentence contains spaces (such as a user ID), they do not need to use quotes or square brackets. This syntax is not difficult to understand, but because it is different from VB, C #, and SQL, errors often occur. Now let's take a moment to break this string apart.
There are two values in this connection string: The database identifier (server, instance, and database) and security values (user ID, password, and installation settings). The database identifier starts with the server value, which is the network name of the machine running SQL Server. (The machine name in XP can be obtained through Start->my Computer, right-click and select Properties->computer name tab->full Computer name.) In Windows 2000, you can right-click->properties->network indentification on the desktop's My computer. If you know that the database server uses the same machine as the Web server that asp.net is running on, you can also specify the server name as (local) to indicate that the server is the native machine where the ASP.net page runs. You can use Instance=myinstance to add an instance. More generally, use MyServer \my-instance or (local) \myinstance to add an instance to the server name. You may also see Using a period (period) to represent a local machine, such as. \SQLExpress.
By default, SSE installs its own instance. A separate reference (local) will result in a failure. You must use (local) \SQLExpress to refer to SSE.
Security settings will be discussed later in this chapter. Now let's take a look at Windows Authentication, using property integratedtrusted_connection=true instead of user ID and Password properties. SQL authentication requires two values: User=myusername;password=mypassword, where MyUserName and MyPassword will be replaced by your certificate. This article is used for Windows authentication.
If you are familiar with an earlier version of ASP, you may be puzzled by the provider. The default provider for SqlDataSource is the. NET Framework Data Provider for SQL Server. Therefore, you do not need to specify a provider in this chapter. The next chapter will discuss specifying NON-DEFAULT providers for other databases.