2012-05-17 23 views
1

我正在創建一個應用程序,只需將數據存儲在數據庫中並從數據庫獲取數據。我正在使用SQLite的數據庫。如何從SQlite和.net 4.0開始?

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(); 
      } 
      } 
    } 

我下載的二進制爲.NET從「http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 「(適用於32位Windows(.NET Framework 4.0))並安裝它。

然後在我的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'."}" 
+0

工作的Visual Studio 2010中 – Swati

+1

有人可以給我確切的步驟,我可以使用SQLite開發簡單的WPF應用程序嗎? – Swati

+0

發佈真正的異常消息(當使用VS進行調試時) – Fabske

回答

4

確保您將項目編譯爲x86而不是AnyCPU。 SQLite的的位數的問題是這麼有名,我經常建議初學者使用C#-SQLite(其中,因爲它是純C#寫的所有模式下正常工作),

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

+0

當我放置> SQLiteConnection myConnection =新的SQLiteConnection();在loadData方法中,它的工作很好,謝謝All。 – Swati

1

看看下面的鏈接。

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

通過使用

dt.DefaultView結合使用WPF數據網格數據轉換數據表到數據視圖;

+0

Jodha我也嘗試了這些步驟,但仍然給出了相同的異常 – Swati

+0

在SQLiteConnection()的構造函數調用的這一行上生成異常。 SQLiteConnection myConnection = new SQLiteConnection(); – Swati

+0

你可以在這裏發佈異常堆棧跟蹤嗎? – JSJ