如何在 DAL 中设计一个方法来从数据库中检索各种结果?

发布于 2024-09-07 06:45:22 字数 909 浏览 1 评论 0原文

我们已经设计了我们的数据访问层,它的一部分工作起来没有任何问题,但我们对设计一种方法有一些疑问,以从我们这里获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隱形的亼 2024-09-14 06:45:22

传入不带“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文