how to design a single methon in DAL to retrieve various result from database?
we have designed our Data Access Layer and parts of it works as a charm without any problem but we have some doubts about designing a met开发者_开发技巧hod to get a reference of sqlcommand from us and return a single row data from database also the retrieved data has an unknown data type here is what I've done :
//I am using this class as retrieval datatype for now i have only 3 returning data types
public class retDataTypes {
public bool ansBool;
public int ansInt;
public string ansString;
}
public class doQuery {
public retDataTypes returnAnswer(ref sqlcommand cmd , string typeHelper) {
//opening the sqlconnection
//pass the cmd then
retDataTypes answer = new retDataTypes();
//in most of the cases we need only one row result so that I'm using executeScalar()
switch (typeHelper) {
case "string" :
answer.ansString = cmd.executescalar();
break;
case "int" :
answer.ansInt = cmd.executescalar();
break;
case "bool" :
answer.ansBool = cmd.executescalar();
break;
}
return answer;
}
I need your opinion about this approach is it good ( I know its not efficient) and is there any better ways than this?
please let me know.
regards.
Pass in the SqlCommand without 'ref'.
Change the return type 'retDataTypes' to 'object' and cast it to the type you expect in the calling function.
精彩评论