Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
How to return array dimensional from SQL Query? or can use 2 dimensional List?
here my code , but I dont have any idea for get column name from SQLiteDataReader
C#
public string[][] Select(string query)
        {
            string[,] data = new string[100, 100];
            int i;
            if (this.OpenConnection() == true)
            { //Create Command
                SQLiteCommand cmd = new SQLiteCommand(query, con);
                //Create a data reader and Execute the command
                SQLiteDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    i++;
                    data["title"][i] = dataReader["title"][i];
                    data["author"][i]= dataReader["author"][i];
                    data["isbn"][i] = dataReader["isbn"][i]; 
                }
                 return data;

                //close Data Reader
                dataReader.Close();
                //close Connection
                this.CloseConnection();
            }
Posted
Comments
syed shanu 3-Nov-14 21:54pm    
what do you want exactly,Why do you store db data to array,You can return as datatable or dataset
PIEBALDconsult 3-Nov-14 22:55pm    
Please define a class and return a List of them. Why, in Bob's name, are you trying to use arrays?! This isn't the '70s!

C#
//create class as below 
public class Book
{
   public string Title { get; set; }

   public string Author { get; set; }
   
   public string Isbn { get; set; }
}

//change your method to return list of books 
//when you call, set the query as "select titlle, author, isbn from yourtable"
public List<Book> Select(string query)
{
  List<Book> books = new List<Book>();
  if (this.OpenConnection() == true)
  { //Create Command
      SQLiteCommand cmd = new SQLiteCommand(query, con);
      //Create a data reader and Execute the command
      SQLiteDataReader dataReader = cmd.ExecuteReader();
      while (dataReader.Read())
      {
          Book book = new Book();
          book.Title = dataReader[0] as string;
          book.Author = dataReader[1] as string;
          book.Isbn  = dataReader[2] as string; 
		  books.Add(book);
      }
       
      //close Data Reader
      dataReader.Close();
      //close Connection
      this.CloseConnection();
	  return books;
  }
}
 
Share this answer
 
v3
Comments
PIEBALDconsult 3-Nov-14 22:57pm    
"book.Title = dataReader.GetString(0)"
All you need is cast
book.Title = (string) dataReader [ 0 ]
DamithSL 3-Nov-14 23:24pm    
why? dataReader.GetString will return string
PIEBALDconsult 4-Nov-14 8:12am    
But it's an unnecessary method call.
DamithSL 4-Nov-14 8:23am    
yeh, it will be issue in case of nullable column. I have change the code. thanks PIEBALDconsult, i didn't get your point when I reading your first comment.
To add more flexibility to DamithSL's solution, consider this:

C#
//create class as below 
public class Book
{
   public string Title  { get; private set; }
   public string Author { get; private set; }
   public string Isbn   { get; private set; }

   public Book ( System.Data.IDataRecord Data )
   {
       Title  = (string) Data [ 0 ] ;
       Author = (string) Data [ 1 ] ;
       Isbn   = (string) Data [ 2 ] ;
   }
}
 
public List<Book> Select(string query)
{
  List<Book> books = new List<Book>();
  if (this.OpenConnection() )
  {
      using ( SQLiteCommand cmd = new SQLiteCommand(query, con) )
      {
        using ( SQLiteDataReader dataReader = cmd.ExecuteReader() )
        {
          while (dataReader.Read())
          {
             books.Add ( new Book ( dataReader ) ) ;
          }
        }
      }

      this.CloseConnection();
  }
  return books;
}


There may be a way to make it generic, but it escapes me at the moment.
 
Share this answer
 
v2
Comments
Maciej Los 4-Nov-14 2:17am    
Nice!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900