我如何使aclass包含方法连接到数据库并返回sqlconnection [英] how i can make aclass contains method connect to a database and return sqlconnection
本文介绍了我如何使aclass包含方法连接到数据库并返回sqlconnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何使一个类包含方法连接到数据库并返回sqlconnection使一个类称为dbconnection添加函数
连接(字符串Connectionstring)以连接到数据库
并返回sqlsonnection添加函数GetDataTable(string sql)
返回datatableAdd函数ExecuteSql(string sql)以运行
sql脚本,然后将所有函数设为静态函数
在采购项目中用于连接数据库的此类.
how i can make aclass contains method connect to a database and return sqlconnectionMake a class call it dbconnection Add function
connection (string Connectionstring )to connect to data base
and return sqlsonnection Add function GetDataTable(string sql)
to return datatableAdd function ExecuteSql(string sql) to run
sql script And please make all functions static functions
This class to use it in connecting DB in the purchasing project
推荐答案
您的问题不是很清楚,但这是我的5美分.
0)上课
1)制定方法
2)查看 ADO.NET [这是有关ADO.NET的一系列文章中的第一篇 [并且请使所有函数成为静态函数"似乎表明您相信我们会为您做功课.您应该已经有足够的信息来开始使用.
Your question is not very clear, yet here is my five cents.
0) Make a class
1) Make a method
2) Look at ADO.NET[^]. This is the first in a series of articles on ADO.NET[^]
To the updated question.
My previous answer still applies. Though this statement: "And please make all functions static functions" seems to indicate you are of the belief that we''ll do your homework for you. You should already have enough information to get started.
我建议您先看一下这篇简单的文章: ^ ].
看一下该图,您将看到DbConnection
,DataAdapter
和DataReader
之间的关系.问题是:您不能从数据提供者抽象连接.它是特定于提供程序的.您需要使用连接字符串和使用的某些特定数据提供程序创建DataAdapter
,DataReader
或同时创建实例.完成此操作后,您可以返回DataReader
作为接口System.Data.IDataReader
的实例,并返回DataAdapter
作为接口System.Data.IDataAdapter
,System.Data.IDbDataAdapter
或抽象类System.Data.Common.DbDataAdapter
的实例(还有更多功能) ),具体取决于您的任务.这些实例与提供程序无关,您可以在任何应用程序中使用它们.
—SA
I would advice to look at this simple article first: Using ADO.NET for beginners[^].
Take a look at the diagram where you see relationships betweenDbConnection
,DataAdapter
andDataReader
. The problem is: you cannot abstract connection from data provider. It is provider-specific. You need to create an instance ofDataAdapter
,DataReader
or both using your connection string and some specific data provider you use. When you do it, you can returnDataReader
as the instance of the interfaceSystem.Data.IDataReader
and theDataAdapter
as the instance of the interfaceSystem.Data.IDataAdapter
,System.Data.IDbDataAdapter
or the abstract classSystem.Data.Common.DbDataAdapter
(with some more capabilities), depending on your task. These instances are agnostic to the provider and you can use them in any application.
—SA
这篇关于我如何使aclass包含方法连接到数据库并返回sqlconnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文