【发布时间】: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