我如何使aclass包含方法连接到数据库并返回sqlconnection [英] how i can make aclass contains method connect to a database and return sqlconnection

查看:89
本文介绍了我如何使aclass包含方法连接到数据库并返回sqlconnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使一个类包含方法连接到数据库并返回sqlconnection使一个类称为dbconnection添加函数
连接(字符串Connectionstring)以连接到数据库
并返回sqlsonnection添加函数GetDataTable(string sql)
返回datatableAdd函数Exec​​uteSql(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.


我建​​议您先看一下这篇简单的文章:
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 between DbConnection, DataAdapter and DataReader. The problem is: you cannot abstract connection from data provider. It is provider-specific. You need to create an instance of DataAdapter, DataReader or both using your connection string and some specific data provider you use. When you do it, you can return DataReader as the instance of the interface System.Data.IDataReader and the DataAdapter as the instance of the interface System.Data.IDataAdapter, System.Data.IDbDataAdapter or the abstract class System.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆