如何在与sql server连接的应用程序中创建公共类和方法? [英] How to create common class and method in application for connection with sql server ?
本文介绍了如何在与sql server连接的应用程序中创建公共类和方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为应用程序创建公共类,我可以在其中为ado.net代码创建一些方法。我可以在属于我的项目或应用程序的任何c#文件中访问该方法。目前我正在每个c#文件上编写连接代码。
请通过示例告诉我
。
I want to create common class for application in which i can create some methods for ado.net code. And I can access that methods in any c# file belongs to my project or application. Currently I am writing connectivity code on every c# file.
Please let me know the Idea with example
.
推荐答案
首先,不要在公共类中存储打开的连接。最好在一次通话期间打开和关闭连接。
但是为什么不创建一个不需要实例化的静态类的类本身。所以简单地说
First of all don't store an open connection in a common class. It's better to open and close the connection during one call.
But what comes to the class itself, why not create a static class which doesn't need to be instantiated. So simply
internal static class MyConnectionUtils [
// here goes your methods
}
使用时
And when using it
void MyMethod(SqlConnection connection) {
// do something
MyConnectionUtils.SomeMethod(connection);
// do something
}
似乎你在寻找 DAL [ ^ ]和BLL [ ^ ]。
有用的链接:
教程18:处理ASP.NET页面中的BLL级和DAL级异常 [ ^ ]
通用数据访问层 - GDA:第一部分 [ ^ ]
Seems you're looking for DAL[^] and BLL[^].
Useful links:
Tutorial 18: Handling BLL-Level and DAL-Level Exceptions in an ASP.NET Page[^]
Generic Data Access Layer - GDA: Part I[^]
这篇关于如何在与sql server连接的应用程序中创建公共类和方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文