0

I am using System.Data.SQLite.Core nuget package in my asp.net core 3 app.

I have a SQLite DB with my table, assuming something like this:

+-----+------------+-----+
| ... | Phone      | ... |
+-----+------------+-----+
| --- | 0123456789 | --- |
+-----+------------+-----+
| --- | 9876543210 | --- |
+-----+------------+-----+

I do a simple query

command = new SQLiteCommand("SELECT * FROM Table", connection);

var reader = await command.ExecuteReaderAsync();

and get data in my reader. But when I try to Istance the values in an object I can read and cast every value to its type, however I can't cast the phone value into a string (I have declared it as a string in my DB), I mean I can cast it using explicit cast

var p = reader[index2];
var t = p.GetType().Name; //--> String
var myobj = new MyObj(othervalues: reader.GetString(index1), phone: (string)reader[index2]);

That is working but why can't I simply do this ?

... phone: reader.GetString(index2)...

And there is a way to do it ?

Thanks for your answer!

Update:

Attribute

11
  • ok but what if I need do in this way ? Commented Dec 5, 2019 at 19:48
  • myObject(guid id, string name, string phone, int count) Commented Dec 5, 2019 at 19:50
  • Maybe the column is nullable and it's failling to get a specific record with a null value. Commented Dec 5, 2019 at 19:54
  • but a phone is not a nuber... if I store smt like '0215556545' then when i read it using GetInt64(index) i will get 215556545 Commented Dec 5, 2019 at 19:54
  • @AndréSanson there is no null value Commented Dec 5, 2019 at 19:55

1 Answer 1

2

So apparently change colum definition from STRING to TEXT seems to work using reader.GetString(index) and allows to store phone number like '0000000001' too, without deleting zeros.

Sign up to request clarification or add additional context in comments.

2 Comments

Glad you found your solution.
Thank you for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.