使用SQLite时,我在Entity Framework中遇到主键问题.SQLite希望在自动增量主键列的VALUES列表中显式为NULL.我实际上没有看过EF上下文中生成的SQL,但我相信它与通常的SQL Server约定一致,即不为自动增量列提供任何值.
根据ADO.NET SQLite提供程序的站点,EF完全受支持,但我找不到任何帮助.有没有办法强制EF为主键值显式插入NULL?
我想做的是以下几点:
using System.Data.SQLite;
using System.IO;
//My SQLite connection
SQLiteConnection myCon;
public void ReadAndOpenDB(string filename)
{
FileStream fstrm = new FileStream(filename, FileMode.Open);
byte[] buf = new byte[fstrm.Length];
fstrm.Read(buf, 0, (int)fstrm.Length);
MemoryStream mstrm = new MemoryStream(buf);
//Do some things with the memory stream
myCon = new SQLiteConnection(/*attach to my memory stream for reading*/);
myCon.Open();
//Do necessary DB operations
}
Run Code Online (Sandbox Code Playgroud)
我不打算写入内存数据库,但我需要能够在连接到程序之前在程序的内存中对文件做一些事情.
我正在使用x86开发visual studio.我想为x32和x64构建我的应用程序.但我需要使用sqlite .net连接器,它有x86应用程序的dll和x64应用程序的另一个dll.当我的配置是x64时,如何配置我的visual studio加载引用?当我的配置是x86时,如何配置我的visual studio?
谢谢,理查德.
我正在尝试使用EF动态创建嵌入式SQLite数据库但是,我无法使其工作,数据库文件永远不会被创建.
我有EF 4.2和最新版本的SQLite
这就是我所拥有的
的app.config
<?xml version="1.0"?>
<configuration>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider"
invariant="System.Data.SQLite"
description=".Net Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="DataContext"
connectionString="Data Source=test.db;Version=3;New=True;"
providerName="System.Data.SQLite" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" />
</startup>
</configuration>
Run Code Online (Sandbox Code Playgroud)
数据库初始化程序(放入一些内容)
class PageDbInitializer : DropCreateDatabaseAlways<PageDB>
{
protected override void Seed(PageDB context)
{
for (int i = 0; i < 10; i++)
{
WebPage page = new WebPage() { Name = "Page" + (i + 1) };
context.Pages.Add(page);
}
base.Seed(context); …Run Code Online (Sandbox Code Playgroud) 我只是学习SQLite,我无法正确编译命令.当我执行以下代码时:
this.command.CommandText = "INSERT INTO [StringData] VALUE (?,?)";
this.data = new SQLiteParameter();
this.byteIndex = new SQLiteParameter();
this.command.Parameters.Add(this.data);
this.command.Parameters.Add(this.byteIndex);
this.data.Value = data.Data;
this.byteIndex.Value = data.ByteIndex;
this.command.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)
我得到一个SQLite异常.在检查CommandText时,我发现无论我做什么都没有正确添加参数:INSERT INTO [StringData] VALUE (?,?)
我缺少什么想法?
谢谢
我正在使用C#导入一个包含6-8百万行的CSV.
我的表看起来像这样:
CREATE TABLE [Data] ([ID] VARCHAR(100) NULL,[Raw] VARCHAR(200) NULL)
CREATE INDEX IDLookup ON Data(ID ASC)
Run Code Online (Sandbox Code Playgroud)
我正在使用System.Data.SQLite来进行导入.
目前在Windows 7 32位,Core2Duo 2.8Ghz和4GB RAM上耗时6分55秒.这不是太糟糕,但我只是想知道是否有人能够更快地看到导入它的方法.
这是我的代码:
public class Data
{
public string IDData { get; set; }
public string RawData { get; set; }
}
string connectionString = @"Data Source=" + Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + "\\dbimport");
System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(connectionString);
conn.Open();
//Dropping and recreating the table seems to be the quickest way to get old data removed
System.Data.SQLite.SQLiteCommand command = new System.Data.SQLite.SQLiteCommand(conn); …Run Code Online (Sandbox Code Playgroud) 在数据库仍处于联机状态时,如何在本机C#代码中备份sqlite数据库?所有在线备份api示例都是C代码.
我正在尝试做一个SQL查询,如
SELECT * FROM [TABLE] WHERE hostname LIKE '%myhostname%';
Run Code Online (Sandbox Code Playgroud)
这在纯SQL中工作正常,但是当我在C#中使用System.Data.SQLite时,它只适用于文字,而不是参数,例如
string sel = "SELECT * FROM [TABLE] WHERE hostname LIKE '%@host%'";
...
command.Parameters.AddWithValue("@host", "myhostname");
Run Code Online (Sandbox Code Playgroud)
这不会返回任何结果.
我的C#/ SQLite工作正常,直到我决定更新SQLite DLL(从1.0.82.0到1.0.84.0).现在我遇到了这个崩溃:
无法在DLL"SQLite.Interop.dll"中找到名为"sqlite3_changes_interop"的入口点
A first chance exception of type 'System.EntryPointNotFoundException' occurred in System.Data.SQLite.dll
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>SparkleShare.vshost.exe</AppDomain><Exception><ExceptionType>System.EntryPointNotFoundException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Unable to find an entry point named 'sqlite3_changes_interop' in DLL 'SQLite.Interop.dll'.</Message><StackTrace> at System.Data.SQLite.UnsafeNativeMethods.sqlite3_changes_interop(IntPtr db)
at System.Data.SQLite.SQLite3.get_Changes()
at System.Data.SQLite.SQLiteStatement.TryGetChanges(Int32&amp; changes)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
Run Code Online (Sandbox Code Playgroud)
代码没有改变,所以我不认为这是一个代码问题(无论如何,这是它发生的行).
我下载的新版本有什么问题吗?我在http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wikisqlite-netFx40-static-binary-Win32-2010-1.0.84.0.zip的Precompiled Statically-Linked Binaries for 32-bit Windows (.NET Framework 4.0)段落中下载了
编辑:我恢复到以前的DLL,但问题仍然发生,可能是因为Visual C#在某处无法清理某些缓存.
(这是一个重复的问题,已在stackoverflow.com中提出.我已经阅读了答案.我已经尝试了解决方案,但这并没有解决我的问题.我将解释我的问题是什么以及有什么问题我做完了).
这是我的问题:
我的应用程序使用System.Data.SQLite.DLL.我引用它并在我的计算机中正常运行,但它无法在另一台计算机上运行.这是错误消息:
System.IO.FileLoadException:无法加载文件或程序集'System.Data.SQLite,Version = 1.0.88.0,Culture = neutral,PublicKeyToken = db937bc2d44ff139'或其依赖项之一.应用程序无法启动,因为它的并排
配置不正确.有关更多详细信息,请参阅应用程序事件日志或使用命令行sxstrace.exe工具.
(来自HRESULT的异常:0x800736B1)文件名:'System.Data.SQLite,Version = 1.0.88.0,Culture = neutral,PublicKeyToken = db937bc2d44ff139'---> System.Runtime.InteropServices.COMException(0x800736B1):应用程序失败开始,因为它的并排
配置不正确.有关更多详细信息,请参阅应用程序事件日志或使用命令行sxstrace.exe工具.
(来自HRESULT的异常:0x800736B1)SimPB.config.PrepareDatabase(),位于SimPB.Program.Main()的SimPB.config.InitializeProgram()
我的电脑运行Windows 7 32位,Visual Studio 2010.
另一台计算机也运行Windows 7 32位,没有安装Visual Studio.
我尝试过的:
尝试1:确保使用Release + x86构建应用程序,并使用目标框架构建应用程序:.NET Framework 2.0.完成.
尝试2:确保使用正确版本的System.Data.SQLite引用该应用程序,该版本是x86 + .Net Framework 2.0(sqlite-netFx20-binary-Win32-2005-1.0.88.0).完成
尝试3:在App.config上尝试这些代码:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
Run Code Online (Sandbox Code Playgroud)
还有这个
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v2.0.50727"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
<NetFx40_LegacySecurityPolicy enabled="true"/>
</runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud)
还有这个
<?xml version="1.0" …Run Code Online (Sandbox Code Playgroud)