SqliteDataReader不';我不能在C#工作吗?
我有一些数据存储在SQLite数据库中,我想用C#显示在网页上。我搜索了正确的操作,但只找到了console.writeline,除此之外,SqliteDataReader函数不起作用。这是我的代码:SqliteDataReader不';我不能在C#工作吗?,c#,html,mysql,database,sqlite,C#,Html,Mysql,Database,Sqlite,我有一些数据存储在SQLite数据库中,我想用C#显示在网页上。我搜索了正确的操作,但只找到了console.writeline,除此之外,SqliteDataReader函数不起作用。这是我的代码: protected void Page_Load(object sender, EventArgs e) { using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(
protected void Page_Load(object sender, EventArgs e)
{
using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection("Data Source=C:/Users/elias/Documents/Visual Studio 2017/WebSites/WebSite7/App_Data/overhoren.db"))
{
using (System.Data.SQLite.SQLiteCommand command = new System.Data.SQLite.SQLiteCommand(conn))
{
conn.Open();
command.Connection = conn;
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
string test = ("Name: " + reader["name"] + "\tScore: " + reader["score"]);
command.ExecuteNonQuery();
conn.Close();
}
}
我该怎么办
提前感谢,
Elias您似乎忘记将实际的查询放入执行:
command.CommandText = "...";
大概是这样的:
protected void Page_Load(object sender, EventArgs e)
{
//TODO: do not hardcode connection string, move it to settings
string connectionString =
@"Data Source=C:/Users/elias/Documents/Visual Studio 2017/WebSites/WebSite7/App_Data/overhoren.db";
// var for simplicity
using (var conn = new System.Data.SQLite.SQLiteConnection(connectionString))
{
conn.Open();
using (var command = new System.Data.SQLite.SQLiteCommand(conn))
{
command.Connection = conn;
//TODO: put the right SQL to perform here
command.CommandText =
@"select name,
score
from MyTable";
using (var reader = command.ExecuteReader()) {
string test = "";
// do we have any data to read?
//DONE: try not building string but using formatting (or string interpolation)
if (reader.Read())
test = $"Name: {reader["name"]}\tScore: {reader["score"]}";
//TODO: so you've got "test" string; do what you want with it
}
}
//DONE: you don't want command.ExecuteNonQuery(), but command.ExecuteReader()
//DONE: you don't want conn.Close() - "using" will do it for you
}
}
你的问题是什么?也就是说,您必须将command.CommandText=“…”代码>