Getting table data into Gridview in C# (SQLite) is not working
我想显示我的数据,这些数据存储在一个ASP.NET页面的sqlite数据库中,并用C编码。我在网上搜索了很多,在我之前的问题中,有人给我看了一篇非常有帮助的文章。我用了代码,但还是不起作用。我想要的是在我的网格视图中得到前三列。因此,表格"tbwoorden"中的"woord"、"vertaling"和"gebruiker"应该显示在网格视图中。这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | 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字符串:
1 2 3 4 | private static SqlDataAdapter NewMethod(System.Data.SQLite.SQLiteConnection conn, string strSelectCmd) { return new SqlDataAdapter(strSelectCmd, conn); } |
我要换什么?提前谢谢,伊莱亚斯
您必须使用