简体   繁体   English

C#中所有请求的SQL Server类?

[英]Sql Server class in C# for all request?

I'm looking for a class for Sql Server. 我正在寻找Sql Server的类。 I need to make insert, update, delete, select (retrieve many rows and columns) and execute Stored Procedure. 我需要进行插入,更新,删除,选择(检索许多行和列)并执行存储过程。

I didn't find a sample of this sort of class and i didn't want to reinvente the wheel. 我没有找到此类课程的样本,也不想重新发明轮子。

Somebody can give it to me? 有人可以给我吗?

You sound like you may be looking for a ORM (Object Relational Mapper) . 您听起来好像正在寻找ORM(对象关系映射器) There are a great number available , some built right it to the .NET framework itself. 大量可用的方法 ,其中一些是建立在.NET框架本身之上的。 Look at the various websites and see if you can find one that fits your needs. 查看各种网站,看看是否可以找到符合您需求的网站。

There's not a single class that does this, but instead a set of a few classes you need to know: 没有一个单独的类可以做到这一点,但是您需要了解一些类:

Sql Server specific: 特定于Sql Server:

System.Data.SqlClient.SqlConnection 
System.Data.SqlClient.SqlCommand
System.Data.SqlClient.SqlDataReader
System.Data.SqlClient.SqlDataAdapter
System.Data.SqlClient.SqlParameter

Used by all database types 由所有数据库类型使用

System.Data.DataTable
System.Data.DataSet
System.Data.SqlDbType (enum) 

There are others as well, but these are the main ones. 也有其他的,但是这些是主要的。 Together, these make up the ADO.Net API, and the Sql Server provider for the ADO.Net API. 这些共同构成了ADO.Net API和ADO.Net API的Sql Server提供程序。

Additionally, there are a number of Object Relational Mappers that build on top of ADO.Net to try to make this easier. 此外,在ADO.Net的基础上建立了许多对象关系映射器,以试图使此过程更容易。 Entity Framework, Linq To Sql, and NHibernate are of a few of the more common options. 实体框架,Linq To Sql和NHibernate是一些较常见的选项。 One common characteristic of ORMs is that they try to free you from even knowing the sql language. ORM的一个共同特征是它们试图使您摆脱甚至不知道sql语言的负担。 If you want to write your own SELECT/INSERT/UPDATE/DELETE queries, which it sounds like you do, you should start at the native ADO.Net level. 如果您想编写自己的SELECT / INSERT / UPDATE / DELETE查询(听起来像您这样做),则应从本机ADO.Net级别开始。

To put your data access in one object, you create your own class that makes use of these other types. 要将数据访问放在一个对象中,您可以创建自己的类,并利用这些其他类型。 Don't try to build a new public method that accepts an sql string. 不要尝试构建一个接受sql字符串的新公共方法。 Build individual methods for each query you will want to run that include the needed sql as part of the method, and have those methods use these types to change or return data. 为要运行的每个查询构建单独的方法,其中包括所需的sql作为方法的一部分,并让这些方法使用这些类型来更改或返回数据。

You might be interested in this tutorial . 您可能对本教程感兴趣。

There is builtin functionality ( System.Data.SqlClient ) to simply access an SQL server. 有内置功能( System.Data.SqlClient )可以简单地访问SQL Server。

There is no single class that can do everything you need. 没有哪门课程可以满足您的所有需求。 Whatever choice you decide you would necessarily need to deal with multiple classes. 无论您选择哪种选择,都必须要处理多个类。

Look at it this way – in order to get data from SQL Server you need to typically do following things: 以这种方式查看-为了从SQL Server获取数据,通常需要执行以下操作:

  • Open connection 打开连接
  • Crete SQL query 克里特SQL查询
  • Execute SQL Query 执行SQL查询
  • Accept results 接受结果
  • Close connection Putting all this functionality into a single class would make the class way too complex. 紧密连接将所有这些功能放在单个类中会使该类的方法过于复杂。

Here is a good reading material for what you need. 这是您所需的一本好书。

Beginners guide to accessing SQL Server through C# 通过C#访问SQL Server的初学者指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM