mysql - Retrieve database result and assign it to a string in C# -


i have below table @ sql server.

table name- userinfo

name    company_id    task1   task2    task3   anna     123           true   true     false neeva    456           false  true     false 

i need know user task1, task2 , task3 status. e.g. anna has true task1 , task2. need below output.

annas details string --> (task1, task2) neeva --> (task2)

now using sql query,

select case when ( substring (value, len(value), 1) = ',') substring (value, 1, (len(value) -1) ) else value end ( select  (case when task1 = 1 'task1,' else ''end) +   (case when task2 = 1 'task2,' else ''end)  +    (case when task3 = 1 'task3,' else ''end) value  userinfo tab 

i following result in sql new column.

(no column name) task1,task2 task1 

instead of creating new column output needed, need pass output c#. e.g. if want know status of anna, query database , check user anna first. once done, check status.

so @ c# side, need this

string userdetails = task1,task2  

this details should show when queried user anna). how can result in c# instead of displaying result in sql column shown above.

you can in c# after have retrieved raw data specific user database. here console app of how approach issue:

create poco hold user info

public class userinfo {     public string name { get; set; }     public string companyid { get; set; }     public bool task1 { get; set; }     public bool task2 { get; set; }     public bool task3 { get; set; } } 

create variable hold test data

static list<userinfo> _userinfo; 

then method hydrate variable

private static void createmockdata()     {         _userinfo = new list<userinfo>();          _userinfo.add(new userinfo { name = "anna", companyid = "123", task1 = true, task2 = true, task3 = false });         _userinfo.add(new userinfo { name = "neeva", companyid = "456", task1 = false, task2 = true, task3 = false });     } 

the important work done in method examine value of objects , return appropriate string

private static string getuserinfobyname(string name)     {         stringbuilder tasks = new stringbuilder();          // call db return raw values user         var userinfo = _userinfo.find(e => e.name == name);          // test tasks having value of true         if(userinfo.task1)         {             tasks.append("task1,");         }          if(userinfo.task2)         {             tasks.append("task2,");         }          if(userinfo.task3)         {             tasks.append("task3");         }          // convert string builder string         var result = tasks.tostring();          // remove last comma if have 1         if(result.endswith(","))         {             result = result.remove(result.length - 1);         }          return result;     } 

here main method puts together

static void main(string[] args)     {         // data in database         createmockdata();          var userdetails = getuserinfobyname("anna");         console.writeline(userdetails);          userdetails = getuserinfobyname("neeva");         console.writeline(userdetails);          console.readline();     } 

the output

task1,task2

task2


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -