c# - Retrieve single value from Database, much like DLookup() in MS Access -
i exploring silverlight (c#) , sqlserver next evolution our current (slow) access database. far has been great, using domainservices retrieve data need. in our database have table (supervisors) supervisor_id, supervisor_firstname, supervisor_lastname , many other fields.
what want recreate function use in current database called entitynamefirstlast(entityid) take integer. retrieve value of [supervisor_firstname] [supervisors] table [supervisor_id] == entityid using following: firstname = dlookup("[supervisor_firstname]", "supervisors", "[supervisor_id] = entityid
i same lastname , combine strings returning 1 string first , last name.
how can single value database through domainservice (or way matter)? understand iqueryable getsupervisorbyid(int supid) return entire row need, how can specific field row?
i aware can set domaindatasource in xaml , bind data want, curious if asked above doable or not.
there number of ways can accomplish requirement if need single value ms-sql server:
1.use query concatenation , use output in code
select supervisor_firstname + ' ' + supervisor_lastname supervisor_fullname supervisors supervisor_id = entityid
now can above query execute through sqlcommand , part thats interesting you
private string getsupervisorfullname(string entityid, string connectionstring) { string query = "select supervisor_firstname + ' ' + supervisor_lastname supervisor_fullname supervisors supervisor_id = @entityid"; string supervisorfullname = ""; using(sqlconnection con = new sqlconnection(connectionstring)) { sqlcommand cmdsupervisorfullname = new sqlcommand(); cmdsupervisorfullname.connection = con; cmdsupervisorfullname.commandtext = query; cmdsupervisorfullname.commandtype = commandtype.text; sqlparameter paraentityid = new sqlparameter(); paraentityid.parametername = "@entityid"; paraentityid.sqldbtype = sqldbtype.nvarchar; paraentityid.direction = parameterdirection.input; paraentityid.value = entityid; cmdsupervisorfullname.parameters.add(paraentityid); try { con.open(); supervisorfullname = (string) cmdsupervisorfullname.executescalar(); } catch(exception ex) { console.writeline(ex.message); } return supervisorfullname; } }
2.second way create scalar function in sql requirement , access function using same kind of method mentioned above.
then take return value method getsupervisorfullname
, populate control value of choice.
please note there again other methods of doing same linqtosql or other orm tools. above 2 methods basic way of accomplishing them.
hope helps.
Comments
Post a Comment