其實我驚訝的是,連接字符串在所有工作。此外,這將是易於使用的String.format建立這種連接字符串:
var filename = @"F:\My Own programs\KrarZara2\KZ\KZ\Kzdb.s3db";
var connString = string.Format("Data Source={0};UseUTF16Encoding=True;", filename);
using(var conn = new SQLiteConnection(connString))
{
...
}
首先,你要替換爲您的設置適當的值UseUTF16Encoding。其次,請注意連接字符串中的文件路徑不包含引號。
如果您正在尋找交換sqlite的數據文件在運行時,你可以看看這個博客條目的手段:
SQLite and Entity Framework 4
解決方案的總結是解析實體框架連接串,更改數據文件,然後重新設置:
public static string RedirectedEntityFrameworkConnectionString(string originalConnectionString, string databaseFile, string password)
{
// Parse the Entity Framework connection string.
var connectionStringBuilder = new EntityConnectionStringBuilder(originalConnectionString);
if (connectionStringBuilder.Provider != "System.Data.SQLite")
{
throw new ArgumentException("Entity Framework connection string does not use System.Data.SQLite provider.");
}
// Parse the underlying provider (SQLite) connection string.
var providerConnectionStringBuilder = new SQLiteConnectionStringBuilder(connectionStringBuilder.ProviderConnectionString);
// Redirect to the specified database file, and apply encryption.
providerConnectionStringBuilder.DataSource = databaseFile;
providerConnectionStringBuilder.Password = password;
// Rebuild the Entity Framework connection string.
connectionStringBuilder.ProviderConnectionString = providerConnectionStringBuilder.ConnectionString;
return connectionStringBuilder.ConnectionString;
}
要使用,你會做這樣的事情:
const string OriginalConnectionString = "..."; // (Copy out of app.config)
var connectionString = RedirectedEntityFrameworkConnectionString(OriginalConnectionString, myFileName, null);
using (var context = new MyEntities(connectionString))
{
...
}
現在他給我的另一個錯誤,不支持關鍵字:「數據源」。我會改變 – 2011-05-08 16:16:29