Database.OpenConnectionString Method (String, String)

Opens a connection to a database using a connection string and the specified provider.

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

public static Database OpenConnectionString(
	string connectionString,
	string providerName


Type: System.String
The connection string that contains information that is used to connect to a database.
Type: System.String
(Optional) The name of the .NET Framework data provider to use to connect to the data source.

Return Value

Type: WebMatrix.Data.Database
The database instance.


connectionString is null or empty.

The OpenConnectionString method differs from the Open method, which uses the name of a connection string that is stored in the Web.config file.

You might use the OpenConnectionString method when a connection string is programmatically generated or is provided by the user.

The following example shows how to use the OpenConnectionString method to connect to a database.

    var connectionString = "Data Source=.\\SQLExpress;Initial Catalog=SmallBakery;Integrated Security=True";
    var providerName = "System.Data.SqlClient";

    var db = Database.OpenConnectionString(connectionString, providerName);

    var selectQueryString = "SELECT * FROM Product ORDER BY Name";

<!DOCTYPE html>

<html lang="en">
        <meta charset="utf-8" />
        <title>Small Bakery Products</title>
            table, th, td {
                border: solid 1px #bbbbbb;
                border-collapse: collapse;
                padding: 2px;
        <h1>Small Bakery Products</h1>
                    @foreach(var row in db.Query(selectQueryString)) {
                        <td >@row.Id</td>
            <a href=@Href("~/InsertProducts")>Add Products</a>

  • Medium trust for the immediate caller. This member can be used by partially trusted code.