2017-05-14 61 views
-1

我想在C#中編寫的ASP.net頁面中顯示我存儲在SQLite數據庫中的數據。
我在互聯網上搜索了很多,在之前的問題中,有人向我展示了一篇非常有用的文章。我使用的代碼,但它仍然無法正常工作。

我想要的是在我的gridview中獲得前三列。因此,應該在gridview中顯示錶「tbWoorden」中的「woord」,「vertaling」和「gebruiker」。
enter image description here

這是我的代碼:在C#中獲取表格數據到Gridview中(SQLite)不起作用

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class Scripts_Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void btnTest_Click(object sender, EventArgs e) 
    { 

     string connectionString = 
     @"Data Source=C:/Users/elias/Documents/Visual Studio 2017/WebSites/WebSite7/App_Data/overhoren.db"; 

     using (var conn = new System.Data.SQLite.SQLiteConnection(connectionString)) 
     { 
      conn.Open(); 

      DataSet dsTest = new DataSet(); 


      // Create a SELECT query. 
      string strSelectCmd = "SELECT woord,vertaling,gebruiker FROM tbWoorden"; 


      // Create a SqlDataAdapter object 
      // SqlDataAdapter represents a set of data commands and a 
      // database connection that are used to fill the DataSet and 
      // update a SQL Server database. 
      SqlDataAdapter da = NewMethod(conn, strSelectCmd); 


      // Fill the DataTable named "Person" in DataSet with the rows 
      // returned by the query.new n 
      da.Fill(dsTest, "tbWoorden"); 


      // Get the DataView from Person DataTable. 
      DataView dvPerson = dsTest.Tables["tbWoorden"].DefaultView; 


      // Set the sort column and sort order. 
      dvPerson.Sort = ViewState["SortExpression"].ToString(); 


      // Bind the GridView control. 
      grdMijnLijsten.DataSource = dvPerson; 
      grdMijnLijsten.DataBind(); 

      using (var command = new System.Data.SQLite.SQLiteCommand(conn)) 
      { 
       command.Connection = conn; 

       command.CommandText = 
        @"SELECT[vertaling], [woord] FROM[tbWoorden] WHERE[woord] = 'ans'"; 



       using (var reader = command.ExecuteReader()) 
       { 
        string test = ""; 



       } 
      } 



     } 
    } 

    private static SqlDataAdapter NewMethod(System.Data.SQLite.SQLiteConnection conn, string strSelectCmd) 
    { 
     return new SqlDataAdapter(strSelectCmd, conn); 
    } 

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 

    } 

    protected void grdMijnLijsten_SelectedIndexChanged(object sender, EventArgs e) 
    { 

    } 
} 

我得到的錯誤是:無法從 'System.Data.SQLite.SQLiteConnection' 到 '字符串' 轉換。
導致錯誤的部分是在NewMethod的康恩字符串:

private static SqlDataAdapter NewMethod(System.Data.SQLite.SQLiteConnection conn, string strSelectCmd) 
{ 
    return new SqlDataAdapter(strSelectCmd, conn); 
} 


我有什麼改變? 由於提前,埃利亞斯

回答

3

你必須使用SQLiteDataAdapter(從SQLite的家庭),而不是SqlDataAdapter(這是SQLCLIENT家族的一部分)

+0

非常感謝,這確實固定它。對於讀這個的其他人:確保你使用的是system.data.SQLite(我忘了) – ielaajez

相關問題