【问题标题】:Getting table data into Gridview in C# (SQLite) is not working在 C# (SQLite) 中将表数据导入 Gridview 不起作用
【发布时间】:2017-10-13 10:03:03
【问题描述】:

我想在我用 C# 编码的 ASP.net 页面中显示我存储在 SQLite 数据库中的数据。
我在互联网上搜索了很多,在我之前的问题中,有人向我展示了一篇非常有用的文章。我使用了代码,但它仍然不起作用。

我想要的是在我的 gridview 中获取前三列。所以表格“tbWoorden”中的“wood”、“vertaling”和“gebruiker”应该显示在gridview中。


这是我的代码:

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' 转换为 'string'。
导致错误的部分是 NewMethod 中的 conn 字符串:

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


我必须改变什么? 在此先感谢,埃利亚斯

【问题讨论】:

    标签: c# sql asp.net sqlite gridview


    【解决方案1】:

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

    【讨论】:

    • 非常感谢,这确实解决了它。对于其他阅读本文的人:确保您使用的是 system.data.SQLite(我忘了)
    猜你喜欢
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 2015-01-24
    • 1970-01-01
    相关资源
    最近更新 更多