我不知道如果我跟着你正確的,但你嘗試這樣的事:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="MyDevArmyDB" connectionString="Data Source=DATASOURCE;Initial Catalog=devarmydb;Persist Security Info=True;User ID=USERID;Password=PASSWORDSTRING"
providerName="System.Data.SqlClient" />
</connectionStrings>
DATASOURCE-替換數據庫 的地址USERID-與實際用戶ID PASSWORDSTRING更換 - 替換實際密碼
訪問DB像這樣做:
public class SqlUserRepository : IUserRepository
{
private DataContext dataContext;
private Table<User> usersTable;
public IQueryable<User> Users { get { return usersTable; } }
public SqlUsersRepository(string databaseName)
{
HttpRequestWrapper request = new HttpRequestWrapper(System.Web.HttpContext.Current.Request);
Configuration config = WebConfigurationManager.OpenWebConfiguration(request.ApplicationPath);
dataContext = new DataContext(config.GetConnetionString(databaseName));
}
}
注意,戈tConnectionString是支持的連接字符串的加密/解密(如果你決定要保護連接字符串)的擴展功能:
public static class ConfigExtension
{
public static string GetConnetionString(this Configuration config, string databaseName, string provider = "RSAProtectedConfigurationProvider")
{
string sectionName = "connectionStrings";
ConfigurationSection section = config.GetSection(sectionName);
if (section != null && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(provider);
config.Save();
}
return WebConfigurationManager.ConnectionStrings[databaseName].ConnectionString;
}
}
我的託管服務提供商是http://www.inside.hr它是來自克羅地亞的託管公司 – 2011-03-15 22:56:06
您是否允許擁有多個數據庫?你不需要僅僅使用一個存儲庫來使用兩個數據庫,但是出於其他原因,這是一個很好的模式。 – 2011-03-15 23:14:04
是的,我可以有更多的一個數據庫 – 2011-03-16 07:49:10