A SQL Server Compact connection string allows you to specify the database name in various ways:
Data Source=c:\data\mydb.sdf
Data Source=mydb.sdf
Data Source=|DataDirectory|\mydb.sdf
But sometimes you need the full path to the database file based on a user defined connection string. This week’s code snippet allows you to do exactly that, and it is a little bit quirky, as it works around a bug in SQL Server Compact 4.0 SP1, that causes use of |DataDirectory| to not be resolved correctly using only the SqlCeConnectionStringBuilder.
public string PathFromConnectionString(string connectionString)
{
SqlCeConnectionStringBuilder sb = new SqlCeConnectionStringBuilder(GetFullConnectionString(connectionString));
return sb.DataSource;
}
public string GetFullConnectionString(string connectionString)
{
using (SqlCeReplication repl = new SqlCeReplication())
{
repl.SubscriberConnectionString = connectionString;
return repl.SubscriberConnectionString;
}
}
Notice that the code above only works with version 4.0, not 3.5
No comments:
Post a Comment