如何在 DAL 中设计一个方法来从数据库中检索各种结果?
我们已经设计了我们的数据访问层,它的一部分工作起来没有任何问题,但我们对设计一种方法有一些疑问,以从我们这里获取 sqlcommand 的引用并从数据库返回单行数据,而且检索到的数据也有一个未知数数据类型 这就是我所做的:
//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;
}
我需要你对这种方法的意见,它好吗(我知道它效率不高),还有比这更好的方法吗?
请告诉我。
问候。
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 method 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
传入不带“ref”的 SqlCommand。
将返回类型“retDataTypes”更改为“object”并将其转换为您在调用函数中期望的类型。
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.