C# 尝试在wpf中创建db时,System.Data.SQLite中引发异常
我正在尝试创建一个SQLite数据库,但在尝试创建它时一直会遇到此异常 下面是一张被抛出的图片: 我在尝试使用finisar时遇到了相同的异常,但决定尝试System.Data.SQLite,我仍然遇到了相同的异常 我认为这与我的App.config文件有关,但在这一点上,我完全不知道 这是我的代码,以防我的代码出错。非常感谢您的帮助C# 尝试在wpf中创建db时,System.Data.SQLite中引发异常,c#,sql,wpf,database,sqlite,C#,Sql,Wpf,Database,Sqlite,我正在尝试创建一个SQLite数据库,但在尝试创建它时一直会遇到此异常 下面是一张被抛出的图片: 我在尝试使用finisar时遇到了相同的异常,但决定尝试System.Data.SQLite,我仍然遇到了相同的异常 我认为这与我的App.config文件有关,但在这一点上,我完全不知道 这是我的代码,以防我的代码出错。非常感谢您的帮助 public partial class MainWindow : Window { public MainWindow() {
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
connectToSQLite();
}
private void connectToSQLite()
{
SQLiteConnection.CreateFile("ExamLog.sqlite");
SQLiteConnection con = new SQLiteConnection("Date Source=ExamLog.sqlite;Version=3");
con.Open();
string sql = "CREATE TABLE EmpInfo (id int, firstName varchar(20), lastName varchar(20)";
SQLiteCommand cmd = new SQLiteCommand(sql, con);
cmd.ExecuteNonQuery();
}
非常感谢
----根据Dour High Arch的请求进行编辑----
以下是例外情况的详细信息:
System.Windows.Markup.XamlParseException occurred
_HResult=-2146233087
_message='The invocation of the constructor on type 'better.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.
HResult=-2146233087
IsTransient=false
Message='The invocation of the constructor on type 'better.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.
Source=PresentationFramework
LineNumber=3
LinePosition=9
StackTrace:
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
InnerException: System.Data.SQLite.SQLiteException
_HResult=-2147467259
_message=SQL logic error or missing database
near ")": syntax error
HResult=-2147467259
IsTransient=false
Message=SQL logic error or missing database
near ")": syntax error
Source=System.Data.SQLite
ErrorCode=1
StackTrace:
at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String& strRemain) in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLite3.cs:line 1052
at System.Data.SQLite.SQLiteCommand.BuildNextCommand() in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteCommand.cs:line 380
at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index) in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteCommand.cs:line 387
at System.Data.SQLite.SQLiteDataReader.NextResult() in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteDataReader.cs:line 1295
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave) in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteDataReader.cs:line 117
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior) in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteCommand.cs:line 805
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(CommandBehavior behavior) in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteCommand.cs:line 853
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery() in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteCommand.cs:line 838
at better.MainWindow.connectToSQLite() in c:\Users\newAcc\Documents\Visual Studio 2013\Projects\better\better\MainWindow.xaml.cs:line 40
at better.MainWindow..ctor() in c:\Users\newAcc\Documents\Visual Studio 2013\Projects\better\better\MainWindow.xaml.cs:line 28
InnerException:
Also, here's my App.xaml:
<Application x:Class="better.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
发生System.Windows.Markup.XamlParseException
_HResult=-2146233087
_message='对与指定绑定约束匹配的类型'better.MainWindow'调用构造函数时引发异常。'行号'3'和行位置'9'。
HResult=-2146233087
IsTransient=false
Message='对与指定绑定约束匹配的类型'better.MainWindow'调用构造函数时引发异常。'行号'3'和行位置'9'。
Source=PresentationFramework
行号=3
LinePosition=9
堆栈跟踪:
在System.Windows.Markup.WpfXamlLoader.Load(XamlReader XamlReader、IXamlObjectWriterFactory writerFactory、Boolean skipJournaledProperties、Object rootObject、XamlObjectWriterSettings设置、Uri baseUri)
InnerException:System.Data.SQLite.SQLiteException
_HResult=-2147467259
_消息=SQL逻辑错误或缺少数据库
near“)”:语法错误
HResult=-2147467259
IsTransient=false
消息=SQL逻辑错误或缺少数据库
near“)”:语法错误
Source=System.Data.SQLite
错误代码=1
堆栈跟踪:
在c:\dev\SQLite\dotnet\System.Data.SQLite\SQLite3.cs中的System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn,String strSql,SQLiteStatement previous,UInt32 timeoutMS,String&stremain)处:第1052行
在c:\dev\SQLite\dotnet\System.Data.SQLite\SQLiteCommand.cs中的System.Data.SQLite.SQLiteCommand.BuildNextCommand()处:第380行
在c:\dev\SQLite\dotnet\System.Data.SQLite\SQLiteCommand.cs中的System.Data.SQLite.SQLiteCommand.GetStatement(Int32索引)处:第387行
在c:\dev\SQLite\dotnet\System.Data.SQLite\SQLiteDataReader.cs中的System.Data.SQLite.SQLiteDataReader.NextResult()处:第1295行
在c:\dev\SQLite\dotnet\System.Data.SQLite\SQLiteDataReader.cs中的System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd,CommandBehavior behave)处:第117行
在c:\dev\SQLite\dotnet\System.Data.SQLite\SQLiteCommand.ExecuteReader(CommandBehavior)中的System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior)处:第805行
在c:\dev\SQLite\dotnet\System.Data.SQLite\SQLiteCommand.ExecuteNonQuery(CommandBehavior)中的System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(CommandBehavior)处:第853行
在c:\dev\SQLite\dotnet\System.Data.SQLite\SQLiteCommand.cs中的System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()处:第838行
在c:\Users\newAcc\Documents\visualstudio 2013\Projects\better\better\MainWindow.xaml.cs中的better.MainWindow.connectToSQLite()处:第40行
在c:\Users\newAcc\Documents\Visual Studio 2013\Projects\better\better\MainWindow.xaml.cs中的better.MainWindow..ctor()处:第28行
内部异常:
另外,这是我的App.xaml:
谢谢你的帮助 您将“数据源”拼错为“日期源”。这就是XAML解析器在运行类的构造函数时引发异常的原因
create table语句中的另一个错误缺少右括号
更正:
SQLiteConnection con = new SQLiteConnection("Data Source=ExamLog.sqlite;Version=3");
string sql = "CREATE TABLE EmpInfo (id int, firstName varchar(20), lastName varchar(20))";
请参阅您发布的屏幕截图中的链接“将异常详细信息复制到剪贴板”?单击该按钮,并在问题中包含详细信息。并发布你的应用程序。xaml。我在编辑下发布了所有内容,谢谢!哈哈哈,我真傻。非常感谢你。但不幸的是,我还是遇到了同样的错误。这是我注意到的第一个错误点,既然您提到它仍然不起作用,那么我在CREATETABLE语句中发现了另一个SQL语法错误。请看我上面编辑过的答案。我建议你在构造函数中设置一个断点,看看它是否命中,然后逐步通过它。我希望这些是唯一的错误,它与你的App.config无关。谢谢你,成功了!我(显然)是sqlite的新手,所以当我看到一个异常的时候,我在谷歌上搜索了一下,然后彻底地想了想,哈哈。很高兴你让它工作起来了。SQL总是一个不明显的错误源。