尝试在wpf中创建数据库时,System.Data.SQLite中抛出异常

时间:2014-05-13 16:13:43

标签: c# sql wpf database sqlite

我正在尝试创建一个SQLite数据库,但在尝试创建它时会不断收到此异常。

以下是投掷内容的图片:http://imgur.com/VLHNO9T

我在尝试使用finisar时遇到了同样的异常,但决定尝试使用System.Data.SQLite并且仍然得到相同的异常!

我认为它与我的App.config文件有关,但在这一点上,我完全无能为力。

这是我的代码,以防我编码错误。非常感谢所有帮助!

 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>

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您的拼写错误的“数据源”为“日期来源”。这就是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))";