如何从 SQlite 和 .net 4.0 开始

本文关键字:开始 net SQlite | 更新日期: 2023-09-27 18:34:23

我正在创建一个应用程序,简单地将数据存储在数据库中并从数据库中获取数据。我正在使用SQLite作为数据库。

主窗口.xaml.cs文件

using System.Data.SQLite;
namespace SQLiteSample
 {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
     public partial class MainWindow : Window
     {
         private SQLiteCommand sql_cmd;
        private DataSet DS = new DataSet();
        private DataTable DT = new DataTable();
        private SQLiteDataAdapter DB;
        string myConnectionString = "Data Source=C:''Documents and Settings''20024509''My     Documents''SQLite''Enum.db";
        SQLiteConnection myConnection = new SQLiteConnection();
        public MainWindow()
        {
             InitializeComponent();
          }
        private void LoadData()
        {
            try
            {
                myConnection.ConnectionString = myConnectionString;
                myConnection.Open();
                sql_cmd = myConnection.CreateCommand();
                string CommandText = "select EmpID,EmpName from Employee_Info";
                DB = new SQLiteDataAdapter(CommandText, myConnection);
                DS.Reset();
                 DB.Fill(DS);
                DT = DS.Tables[0];
                // grid1.DataSource = DT;
                 myConnection.Close();
             }
             catch (Exception e)
             {
                 myConnection.Close();
             }
           }

         private void SaveData()
         {
             SQLiteConnection myConn = new SQLiteConnection(myConnectionString);
             string myInsertQuery = "INSERT INTO Employee_Info(EmpID,EmpName,EmpAge,EmpSalary) Values(503453535, 'DEVELOPMENT',32987654)";
             SQLiteCommand sqCommand = new SQLiteCommand(myInsertQuery);
              sqCommand.Connection = myConn;
              myConn.Open();
             try
             {
                 sqCommand.ExecuteNonQuery();
              }
              finally
             {
                  myConn.Close();
              }
             }
          private void  FromDB_Click(object sender, RoutedEventArgs e)
           {
             LoadData();
           }
           private void ToDB_Click(object sender, RoutedEventArgs e)
          {
              SaveData();
          }
          }
  }

我从"http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki"(32位Windows的安装程序(.NET Framework 4.0))下载了Binary for .net并安装了它。

然后在我的 wpf 应用程序中添加了引用。但它仍然给了例外

"{"'The invocation of the constructor on type 'SQLiteSample.MainWindow' 
that matches the specified binding constraints threw an exception.' 
Line number '3' and line position '9'."}"

如何从 SQlite 和 .net 4.0 开始

确保将项目编译为 x86 而不是 AnyCPU。SQLite的位数问题非常有名,我经常建议初学者使用C#-sqlite(它在所有模式下都可以正常工作,因为它是用纯C#编写的),

http://code.google.com/p/csharp-sqlite/

看看下面的链接。

http://web.archive.org/web/20100208133236/http://www.mikeduncan.com/sqlite-on-dotnet-in-3-mins/

将数据与 WPF 数据网格绑定 使用 将数据表转换为数据视图

DT.默认视图;