Back to Devexpress

MSSqlConnectionProvider.GetConnectionStringWithAttachForLocalDB(String, String, String) Method

xpo-devexpress-dot-xpo-dot-db-dot-mssqlconnectionprovider-dot-getconnectionstringwithattachforlocaldb-x28-system-dot-string-system-dot-string-system-dot-string-x29.md

latest4.8 KB
Original Source

MSSqlConnectionProvider.GetConnectionStringWithAttachForLocalDB(String, String, String) Method

Returns the connection string to SQL Server 2012 Express LocalDB for the specified server, database and database file location.

Namespace : DevExpress.Xpo.DB

Assembly : DevExpress.Xpo.v25.2.dll

NuGet Package : DevExpress.Xpo

Declaration

csharp
public static string GetConnectionStringWithAttachForLocalDB(
    string server,
    string database,
    string attachDbFilename
)
vb
Public Shared Function GetConnectionStringWithAttachForLocalDB(
    server As String,
    database As String,
    attachDbFilename As String
) As String

Parameters

NameTypeDescription
serverString

A string that is the server name.

| | database | String |

A string that is the database name.

| | attachDbFilename | String |

A string that is the path to the MDB file.

|

Returns

TypeDescription
String

A string that is the connection string to SQL Server 2012 Express LocalDB for the specified server, database and database file location.

|

Remarks

An example of creating the connection string to the local server instance using the absolute path to MDB file:

csharp
MSSqlConnectionProvider.GetConnectionStringWithAttachForLocalDB(
    "(localdb)\\v11.0", "DatabaseName", "D:\\Data\\DatabaseName.mdf");
vb
MSSqlConnectionProvider.GetConnectionStringWithAttachForLocalDB( _
"(localdb)\v11.0", "DatabaseName", "D:\Data\DatabaseName.mdf")

An example of creating the connection string to the local server instance using the path to MDB file located in the DataDirectory folder:

csharp
MSSqlConnectionProvider.GetConnectionStringWithAttachForLocalDB(
    "(localdb)\\v11.0", "DatabaseName", "|DataDirectory|\\DatabaseName.mdf");
vb
MSSqlConnectionProvider.GetConnectionStringWithAttachForLocalDB( _
"(localdb)\v11.0", "DatabaseName", "|DataDirectory|\\DatabaseName.mdf")

To connect to the local server instance when the database files are located in the %USERPROFILE% folder, use the MSSqlConnectionProvider.GetConnectionString method instead of GetConnectionStringWithAttachForLocalDB :

csharp
MSSqlConnectionProvider.GetConnectionString("(localdb)\\v11.0", "DatabaseName");
vb
MSSqlConnectionProvider.GetConnectionString("(localdb)\\v11.0", "DatabaseName")

To connect to a custom LocalDB instance, pass the server parameter in the following notation.

Refer to the SQL Server 2012 Express LocalDB topic in MSDN to learn more about naming LocalDB instances. You do not need to use the SqlLocalDB.exe console tool provided by Microsoft to handle instances, as XPO includes the MSSqlLocalDBApi helper class that wraps the API described in the SQL Server Express LocalDB Instance API Reference document.

See Also

MSSqlConnectionProvider Class

MSSqlConnectionProvider Members

DevExpress.Xpo.DB Namespace