C#中的Sql Server类对于所有请求?

时间:2021-10-31 08:14:22

I'm looking for a class for Sql Server. I need to make insert, update, delete, select (retrieve many rows and columns) and execute Stored Procedure.

我正在寻找一个Sql Server的类。我需要插入,更新,删除,选择(检索许多行和列)并执行存储过程。

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?

有人可以给我吗?

4 个解决方案

#1


2  

You sound like you may be looking for a ORM (Object Relational Mapper). There are a great number available, some built right it to the .NET framework itself. Look at the various websites and see if you can find one that fits your needs.

你听起来像是在寻找ORM(对象关系映射器)。有很多可用的,有些是建立在.NET框架本身的。查看各种网站,看看是否能找到符合您需求的网站。

#2


2  

There's not a single class that does this, but instead a set of a few classes you need to know:

没有一个类可以做到这一点,而是需要知道的一些类:

Sql Server specific:

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. Entity Framework, Linq To Sql, and NHibernate are of a few of the more common options. One common characteristic of ORMs is that they try to free you from even knowing the sql language. 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.

此外,还有许多构建在ADO.Net之上的对象关系映射器,以使这更容易。实体框架,Linq To Sql和NHibernate是一些更常见的选项。 ORM的一个共同特征是它们试图让你甚至不知道sql语言。如果你想编写自己的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. 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字符串的新公共方法。为要运行的每个查询构建单独的方法,包括所需的sql作为方法的一部分,并让这些方法使用这些类型来更改或返回数据。

#3


1  

You might be interested in this tutorial.

您可能对本教程感兴趣。

There is builtin functionality (System.Data.SqlClient) to simply access an SQL server.

有内置功能(System.Data.SqlClient)来简单地访问SQL服务器。

#4


0  

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

#1


2  

You sound like you may be looking for a ORM (Object Relational Mapper). There are a great number available, some built right it to the .NET framework itself. Look at the various websites and see if you can find one that fits your needs.

你听起来像是在寻找ORM(对象关系映射器)。有很多可用的,有些是建立在.NET框架本身的。查看各种网站,看看是否能找到符合您需求的网站。

#2


2  

There's not a single class that does this, but instead a set of a few classes you need to know:

没有一个类可以做到这一点,而是需要知道的一些类:

Sql Server specific:

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. Entity Framework, Linq To Sql, and NHibernate are of a few of the more common options. One common characteristic of ORMs is that they try to free you from even knowing the sql language. 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.

此外,还有许多构建在ADO.Net之上的对象关系映射器,以使这更容易。实体框架,Linq To Sql和NHibernate是一些更常见的选项。 ORM的一个共同特征是它们试图让你甚至不知道sql语言。如果你想编写自己的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. 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字符串的新公共方法。为要运行的每个查询构建单独的方法,包括所需的sql作为方法的一部分,并让这些方法使用这些类型来更改或返回数据。

#3


1  

You might be interested in this tutorial.

您可能对本教程感兴趣。

There is builtin functionality (System.Data.SqlClient) to simply access an SQL server.

有内置功能(System.Data.SqlClient)来简单地访问SQL服务器。

#4


0  

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