Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9066155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:40:34+00:00 2026-06-16T16:40:34+00:00

I’m querying a database with SQLite. I’m trying to get data from this database,

  • 0

I’m querying a database with SQLite. I’m trying to get data from this database, save it in an array, then return to the controller. I need to present this data using a foreach loop in my view.

string sql = "select * from Tasks Where UserId = " + userId.ToString();
using (SQLiteConnection conn = new SQLiteConnection(connString))    
{
    SQLiteCommand cmd = new SQLiteCommand(sql, conn);
    conn.Open();
    using (SQLiteDataReader rdr = cmd.ExecuteReader()) 
    {
        int i = 0;
        while (rdr.Read()) 
        {
            //here is what i would do in PHP
            $array[$i]['name'] = $rdr[i]["name"];
            $array[$i]['key']  $rdr[$i]["key"];
        }
    }
}

return array;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:40:35+00:00Added an answer on June 16, 2026 at 4:40 pm

    Firstly, learn to use parameterised queries not string concatenation as this will help prevent SQL injection attacks.

    string sql = "select * from Tasks Where UserId = @userId";
    

    Also, if you create a class to represent a record in your Tasks table then you can build instances of this object and return them in a list which will make using the code easier since instead of having untyped arrays, you will have an object with properties (in your view you can do foreach (var task in Model) where Model is a List<Task>.

    public class Task
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Key { get; set; }
    }
    
    var tasks = new List<Task>(); // create a list to populate with tasks
    
    using (var connection = new SQLiteConnection(connString))    
    {
        var command = new SQLiteCommand(sql, conn);
        command.Parameters.Add("@userId", userId); // only do .ToString on userId if the column is a string.
    
        connection.Open();
        using (var reader = cmd.ExecuteReader()) 
        {
            while (reader.Read()) 
            {
                var task = new Task();
                task.Id = (int)reader["id"]; // the name of the column in the reader will match the column in the Tasks table.
                task.Name = (string)reader["name"];
                task.Key = (string)reader["key"];
    
                tasks.Add(task);
            }
        }
    }
    
    return tasks;
    

    Instead of writing all the query logic and creating objects, you can use frameworks called Object Relational Mappers (ORM) to do this for you. There are a number of them around, some more simple than others. A MicroORM may suit your purposes, they are simple and easy to use (I have built one called MicroLite but there are others such as dapper or PetaPoco. If you want something more powerful, NHibernate and Entity Framework are popular choices.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a view passing on information from a database: def serve_article(request, id): served_article
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.