1

我正在创建一个应用程序,我只是将数据存储在 DB 中并从 DB 获取数据。我正在将 SQLite 用于数据库。

MainWindows.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”(Setups for 32-bit 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'."}"
4

2 回答 2

5

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

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

于 2012-05-17T13:32:05.777 回答
2

看看下面的链接。

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

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

dt.DefaultView;

于 2012-05-17T12:05:26.590 回答