c# - Return a string from a method that uses a method as a parameter -


i have sql template method want return string variety of methods execute queries want string from:

private string sqlqueryreturnstring(action<sqlconnection> sqlmethod) {     string result = "";     sqlconnection conn = new sqlconnection();     conn.connectionstring = configurationmanager.connectionstrings["applicationservices"].connectionstring;     try     {         //open sql connection         conn.open();         result = sqlmethod(conn);     }     catch (exception ex)     {         system.diagnostics.debug.write(ex.tostring());     }         {         conn.close();     }     return result; }  //get primary key of logged in user private string getpkofuserloggedin(sqlconnection conn) {     int result = 0;     sqlcommand getpkofuserloggedin = new sqlcommand("select [pk_user] [user] [loginname] = @useridparam", conn);     //create , assign parameters     getpkofuserloggedin.parameters.addwithvalue("@useridparam", user.identity.name);     //execute command , retrieve primary key above insert , assign variable     result = (int)getpkofuserloggedin.executescalar();     return result.tostring(); } 

above how think approached. call:

string test = sqlqueryreturnstring(getpkofuserloggedin); 

this part not working:

result = sqlmethod(conn); 

it appears sqlmethod presumed void.

what do functionality want?

you want func<sqlconnection, string>. action void methods, func methods return something.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -