Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Database.Open Method

Opens a connection to a database using the specified file name or using the named connection string.

Namespace:  WebMatrix.Data
Assembly:  WebMatrix.Data (in WebMatrix.Data.dll)

public static Database Open(
	string name
)

Parameters

name
Type: System.String
The name associated with the database to open. name can specify an .sdf or .mdf database file that is in the App_Data folder. (Do not include the file-name extension.) Alternatively, name can specify the name of a connection string in the Web.config file.

Return Value

Type: WebMatrix.Data.Database
The database instance.

The Open(String) method can connect to a database in two ways. The Open(String) method first searches the Web application's App_Data folder for a database file that matches the name that is passed (without the file-name extension) to the method. If no matching database file is found, the method searches the application's Web.config file for a connection string that matches name. This two-step lookup process makes it practical to test the Web application by using a local database file and to later deploy the unaltered application to a hosting environment. For deployment, you can create a connection string that has the name that you specified in name and that specifies the production database. (In that case, you do not deploy the database file that is in the App_Data folder.)

A connection string contains information about how to connect to a database. This can include a file path, or it can include the name of a SQL Server database on a local or remote server, together with a user name and password to connect to that server. If you keep data in a centrally managed version of SQL Server, such as on a hosting provider's site, you always use a connection string to specify the database connection information.

  • Medium trust for the immediate caller. This member can be used by partially trusted code.
Show:
© 2015 Microsoft