使用C#(SQLite)将表数据导入Gridview无效

时间:2017-05-14 15:19:31

标签: c# sql asp.net sqlite gridview

我想在我在C#中编码的ASP.net页面中显示我存储在SQLite数据库中的数据。
我在互联网上搜索了很多,在我之前的问题中有人向我展示了一篇非常有用的文章。我使用了代码,但它仍然无法正常工作
我想要的是获得gridview中的前三列。所以" woord"," vertaling"和" gebruiker"从表格" tbWoorden"应显示在gridview中 enter image description here

这是我的代码:

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中的conn字符串:

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


我需要改变什么? 在此先感谢,Elias

1 个答案:

答案 0 :(得分:3)

您必须使用SQLiteDataAdapter(来自SQLite系列)而不是SqlDataAdapter(它是SQLClient系列的一部分)