Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SqliteDataReader不';我不能在C#工作吗?_C#_Html_Mysql_Database_Sqlite - Fatal编程技术网

SqliteDataReader不';我不能在C#工作吗?

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(

我有一些数据存储在SQLite数据库中,我想用C#显示在网页上。我搜索了正确的操作,但只找到了console.writeline,除此之外,SqliteDataReader函数不起作用。这是我的代码:

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=“…”