I have a sqlite database, I make a request to it in the method, and I can’t figure out how to correctly return the delimited string.

public string GetImportedFileList(string connstring, string idrecord,string tablename,string Idname) { string ImportedFiles = null; using (SQLiteConnection connect = new SQLiteConnection(connstring)) { connect.Open(); using (SQLiteCommand fmd = connect.CreateCommand()) { var sb= new StringBuilder(); var que = sb.Append("SELECT * FROM "+tablename+" where "+Idname+"="+idrecord); fmd.CommandText = que.ToString(); fmd.CommandType = CommandType.Text; SQLiteDataReader r = fmd.ExecuteReader(); while (r.Read()) { object[] values = new object[] { }; r.GetValues(values); ImportedFiles= String.Join(",", values).ToString(); } } } return ImportedFiles; } 

To the input of the method I submit a connection string, the name of the table, the name of the field I need, and the value of this field. And at the output I want to see the result of the query as a string with a separator.

The string will always be the same, since id is unique in the database. I want to get in the row the values ​​of all columns separated by a comma or another separator.

  • one
    Give an example of a method call and a string that should turn out. There will always be one row and you go to return the values ​​of all columns separated by commas? Or will there always be one column and need to return the values ​​of all rows? Or a lot of columns and rows and all you need through the separator? - default locale
  • @defaultlocale Thanks for the answer, but I want to get the values ​​of all columns in a row, separated by a comma or another separator - Vladimr Vladimirovoch
  • one
    And if there are a few lines? - default locale
  • @defaultlocale will always be one line, since id is unique in the database - Vladimr Vladimirovoch

1 answer 1

You can get the values ​​of all columns via GetValues and connect them via String.Join :

 ... while (r.Read()) { object[] values = new object[r.FieldCount]; r.GetValues(values); return String.Join(",", values); } ... 

All values ​​will be read as object and converted to a string via ToString() by default. If you need to output in some special string representation, you will have to select values ​​by looping one by one through GetValue .

I advise you to add a check that the query returned exactly one line, in order to avoid misunderstandings.

Still, usually when writing queries through string concatenation, it is advised to study working with parameterized queries in order to avoid SQL injection. But in this case, the query has a dynamic structure and it will only be possible to parameterize idrecord . In any case, think about security if any method arguments come from outside.

  • Alas, it did not work, the empty line is obtained, the code updated in the question - Vladimr Vladimirovoch
  • @VladimrVladimirovoch I apologize, I forgot to initialize the array. Updated the answer, try again. - default locale
  • one
    Thank you earned - Vladimr Vladimirovoch