JDBC 和 ADO.Net:API 比较

发布于 2024-09-28 22:50:00 字数 480 浏览 0 评论 0原文

JDBC 以及 ADO.Net

我知道 JDBC 和 ADO.Net 中的对象模型并不完全相同,但我认为它们之间可以找到一些类比(以及值得说明的关键区别)。

这对于那些了解一种 API 并想要学习另一种 API 的人来说非常有用,可以作为一个起点,或者避免由于对想要学习的 API 做出的假设而引起的误解。

例如:哪个 ADO.Net 对象提供与 JDBC ResultSet 相同的功能/行为?对于PreparedStatemes 也是如此...

What are the analogies between the objects found in JDBC and the ones found in ADO.Net?

I know the object model in JDBC and ADO.Net are not exactly the same, but I think some analogies can be found among them (and key differences worth stating).

That would be useful for those who knows one API and wants to learn the other, serving as a starting point maybe, or avoiding misunderstandings caused by assumptions one makes about the API that wants to learn.

e.g.: Which is the ADO.Net object that provides the same functionality/behavior as the JDBC ResultSet? the same for PreparedStatemes, and so on...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅一夜美梦 2024-10-05 22:50:00

这是 ADO.NET 的一个简单序列:

// 1. create a connection
SqlConnection conn = new SqlConnection(xyz)
// 2. open the connection
conn.Open();
// 3. create a command
 SqlCommand cmd = new SqlCommand("select * from xyz", conn);
// 4. execute and receive the result in a reader
SqlDataReader rdr = cmd.ExecuteReader();
// 5. get the results
while (rdr.Read())
{
//dosomething
}

这是 JDBC 的一个简单序列:

// 1. create a connection
Connection con = DriverManager.getConnection(xyz);
// 2. create a statement     
Statement stmt = con.createStatement();
// 3. execute and receive results in a result set
ResultSet rs = stmt.executeQuery("SELECT * from xyz");
// 4. Get the results
while (rs.next()) 
{
// do something
}

这是类比(ADO.NET => JDBC):

SqlConnection => Connection
SqlCommand => Statement
SqlDataReader => ResultSet

Here is a simple sequence for ADO.NET:

// 1. create a connection
SqlConnection conn = new SqlConnection(xyz)
// 2. open the connection
conn.Open();
// 3. create a command
 SqlCommand cmd = new SqlCommand("select * from xyz", conn);
// 4. execute and receive the result in a reader
SqlDataReader rdr = cmd.ExecuteReader();
// 5. get the results
while (rdr.Read())
{
//dosomething
}

Here is a simple sequence for JDBC:

// 1. create a connection
Connection con = DriverManager.getConnection(xyz);
// 2. create a statement     
Statement stmt = con.createStatement();
// 3. execute and receive results in a result set
ResultSet rs = stmt.executeQuery("SELECT * from xyz");
// 4. Get the results
while (rs.next()) 
{
// do something
}

And here is the analogy (ADO.NET => JDBC):

SqlConnection => Connection
SqlCommand => Statement
SqlDataReader => ResultSet
耶耶耶 2024-10-05 22:50:00

对于 jdbc 不是很彻底,但据我所知,ADO.NET 遵循断开连接的体系结构,仅在必须执行或读取查询时才建立连接。一旦阅读器被读取,连接就可以关闭。数据缓存是使用数据集和数据适配器来实现的。在 ADO.NET 中,每个连接仅允许一个读取器。虽然在 jdbc 中断开连接的架构当然是可能的,但它建立在实时连接的概念之上,每个连接可以有多个读取器。

API 中的另一个区别是 有内置功能在jdbc中获取最后插入的id,而ADO则缺少。

还阅读了有关 ADO 和 jdbc 中数据缓存的精彩比较.

Not very thorough with jdbc, but from what I know ADO.NET follows a disconnected architecture, where a connection is established only for the time a query has to be executed or read. Once the reader is read, connection can be closed. The data caching are achieved using datasets and data adapters. In ADO.NET only one reader is allowed per connection. While disconnected architecture is certainly possible in jdbc, its built on the concept of having live connection where you can have multiple readers per connection.

Another difference in the API is that there is built in functionality in jdbc to get the last inserted id, while ADO lacks one.

Also read a nice comparison on data caching in ADO and jdbc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文