在gridview(C#)中获取SQLite数据不起作用

时间:2017-05-14 11:15:27

标签: c# asp.net sqlite gridview datagridview

我有一个包含数据的SQLite数据库。我想在我的asp.NET网页上的GridView列表中显示这些数据。 我找到了here的方法,但这对我不起作用。我想要的是在gridview列表中显示我表格的前三列。
my table
所以我想在“woord”,“vertaling”和“列”中显示数据gebruiker”。

这是我的代码:

using System;
using System.Collections.Generic;
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();

            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 = "";

                    if (reader.Read())
                        dataGridView1.Rows.Add(new object[] {
                            read.GetValue(read.GetOrdinal("woord")),  // Or column name like this
                            read.GetValue(read.GetOrdinal("vertaling")),
                            read.GetValue(read.GetOrdinal("gebruiker"))
                        });

                }
            }

        }
    }



    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void grdMijnLijsten_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

这对我不起作用。我认为我做的事情完全错了,但我在网上搜索了很多,找不到解决问题的方法。

我该怎么办?

谢谢,

利亚

2 个答案:

答案 0 :(得分:1)

您忘记在gebruiker命令中指定列SELECT

要获取使用while循环所需的大量数据。

在循环之前,应该获得一次序数值。

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


using (var reader = command.ExecuteReader())
{
    int woordIndex = reader.GetOrdinal("woord");
    int vertalingIndex = reader.GetOrdinal("vertaling");
    int gebruikerIndex = reader.GetOrdinal("gebruiker");

    while (reader.Read())
        dataGridView1.Rows.Add(new object[] {
            reader.GetValue(woordIndex),
            reader.GetValue(vertalingIndex),
            reader.GetValue(gebruikerIndex)
        });
}

答案 1 :(得分:0)

您可以使用此功能。

  private static DataTable ReadSqliteTable(string DBfile, string tableName,string wherestatement = "")
    {
        try
        {
            using (var conn = new SQLiteConnection("Data Source=" + DBfile))
            {
                if (conn.State != ConnectionState.Open) conn.Open();
                SQLiteDataAdapter sQLiteData = new SQLiteDataAdapter("SELECT * FROM " + tableName + wherestatement, conn);
                DataTable table = new DataTable(tableName);
                sQLiteData.Fill(table);
                return table;
            }
        }
        catch { return null; };           
    }