2013-04-18 63 views
2

我是新來的c#和我想讀取.db文件,我已經添加了SQLite的ADO.NET 2.0提供程序到我的項目幫助下從這個鏈接:http://sourceforge.net/projects/sqlite-dotnet2/。但是,我在運行時出錯,無法理解問題。SQLite C#WPF讀取錯誤

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Data; 
using System.Diagnostics; 
using System.Data.SQLite; 


namespace WpfApplication1.Screens 
{ 
    /// <summary> 
    /// Interaction logic for FirstScreen.xaml 
    /// </summary> 
    public partial class FirstScreen : UserControl, ISwitchable 
    { 

     public FirstScreen() 
     { 
      InitializeComponent(); 

      Debug.WriteLine("Send to debug output."); 


         String connString = "Data Source=C:\\projects.db"; 
      using (SQLiteConnection conn = new SQLiteConnection(connString)) 
      { 
       StringBuilder query = new StringBuilder(); 
       query.Append("SELECT App "); 
       query.Append("FROM Alqueva "); 

       using (SQLiteCommand cmd = new SQLiteCommand(query.ToString(), conn)) 
       { 
        conn.Open(); 
        using (SQLiteDataReader dr = cmd.ExecuteReader()) 
        { 
         while (dr.Read()) 
         { 
          Debug.WriteLine(dr.GetValue(0) + " " + dr.GetValue(1) + " " + dr.GetValue(2)); 
         } 
        } 
       } 
      } 
      //Console.ReadLine(); 

     } 

    } 
} 

錯誤:

System.Windows.Markup.XamlParseException was unhandled 
    Message='The invocation of the constructor on type 'WpfApplication1.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.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri) 
     at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) 
     at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) 
     at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) 
     at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc) 
     at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties) 
     at System.Windows.Application.DoStartup() 
     at System.Windows.Application.<.ctor>b__1(Object unused) 
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
     at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
     at System.Windows.Threading.DispatcherOperation.InvokeImpl() 
     at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) 
     at System.Threading.ExecutionContext.runTryCode(Object userData) 
     at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Windows.Threading.DispatcherOperation.Invoke() 
     at System.Windows.Threading.Dispatcher.ProcessQueue() 
     at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
     at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
     at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
     at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
     at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) 
     at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
     at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 
     at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
     at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
     at System.Windows.Application.RunDispatcher(Object ignore) 
     at System.Windows.Application.RunInternal(Window window) 
     at System.Windows.Application.Run(Window window) 
     at System.Windows.Application.Run() 
     at WpfApplication1.App.Main() in C:\Users\User\documents\visual studio 2010\Projects\WpfApplication1\WpfApplication1\obj\x86\Debug\App.g.cs:line 0 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: System.IO.FileLoadException 
     Message=Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information. 
     Source=WpfApplication1 
     StackTrace: 
      at WpfApplication1.Screens.FirstScreen..ctor() 
      at WpfApplication1.MainWindow..ctor() in C:\Users\User\documents\visual studio 2010\Projects\WpfApplication1\WpfApplication1\MainWindow.xaml.cs:line 17 
     InnerException: 

回答

1

你的sqlite的連接必須是這樣:

Data Source=name.db;Version=3; 

Connection strings for SQLite

In App.config add

<startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0"/> 
</startup> 
1

添加到您的Web /應用程序配置:

<startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0"/> 
</startup>