Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of developing Database Independent Application with ADO.NET

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you an example analysis of database-independent applications developed by ADO.NET. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Database independence (DB Independ) means that an application does not depend on a database (such as SqlServer) and can simply (no longer compile) switch to another database (such as Oracle). Database independence is an indicator of the system.

There are many ways to implement database-independent applications, such as NHibernate, EntityFramework, and so on.

Editor introduces the most basic way, the following step by step to complete a simple database-independent Mini Program.

New project

Create a new Console Application in Visual Studio:

(VS 2010 can choose a different .net version, such as 2.0 above, or you can choose another version, which has little impact on this example)

After the project is created, add a reference to System.Configuration as follows:

Add another configuration file to the project:

The project is ready, and the structure is as follows:

(for some reason, the letters of configuration in System.configuration are lowercase.)

Add a connection string to the configuration file

ConnectionString= "server=localhost; user id=sa; password=*; database=northwind" providerName= "System.Data.SqlClient" / > connectionString= "server=localhost; user id=sa; password=*; database=northwind" providerName= "System.Data.SqlClient" / >

Notice that the connection characters are written in the connectionStrings section above. Cannot be written in appSettings because the providerName attribute is used in the program.

Write database-independent code

Three namespaces are referenced in Program.cs:

UsingSystem.Data; usingSystem.Data.Common; usingSystem.Configuration

First, read out the connection string information in the configuration file:

ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings ["default"]

The factory mode is used irrespective of the ADO.NET data, which can be obtained according to the providerName in the configuration:

DbProviderFactory factory = DbProviderFactories.GetFactory (settings.ProviderName)

DbProviderFactory provides several methods to help us create objects that access the database as needed:

Public abstract class DbProviderFactory {/ /... PublicvirtualDbCommand CreateCommand (); publicvirtualDbCommandBuilder CreateCommandBuilder (); publicvirtualDbConnection CreateConnection (); publicvirtualDbConnectionStringBuilder CreateConnectionStringBuilder (); publicvirtualDbDataAdapter CreateDataAdapter (); publicvirtualDbDataSourceEnumerator CreateDataSourceEnumerator (); publicvirtualDbParameter CreateParameter ();}

DbConnection, DbCommand, DbDataAdapter, DbParameter and so on are all database-independent.

Here are a few small examples of the use of these objects:

1. Query the number of employees

Using (DbConnection connection = factory.CreateConnection ()) {connection.ConnectionString = settings.ConnectionString; DbCommand command = connection.CreateCommand (); command.CommandType = CommandType.Text; command.CommandText = "select count (*) from employees"; connection.Open (); varobj = command.ExecuteScalar (); if (obj isint) employeesCount = (int) obj; elseemployeesCount = (long) obj; connection.Close (); Console.WriteLine ("total staff {0}", employeesCount)

Count (*) different database returns different types of values (SqlServer returns Int32,MySql returns Int64), and 10-12 rows are processed.

two。 Use DbDataReader

Using (DbConnection connection = factory.CreateConnection ()) {connection.ConnectionString = settings.ConnectionString; DbCommand command = connection.CreateCommand (); command.CommandType = CommandType.Text; command.CommandText = "select * from employees"; connection.Open (); DbDataReader reader = command.ExecuteReader (); while (reader.Read ()) Console.WriteLine ("{0} {1}", reader [FirstName "], reader [" LastName "]); connection.Close ();}

3. Populate Dataset

DataSet dataSet = newDataSet (); using (DbConnection connection = factory.CreateConnection ()) {connection.ConnectionString = settings.ConnectionString; DbCommand command = connection.CreateCommand (); command.CommandType = CommandType.Text; command.CommandText = "select * from employees"; DbDataAdapter adapter = factory.CreateDataAdapter (); adapter.SelectCommand = command; connection.Open (); adapter.Fill (dataSet, "Employees"); connection.Close ();}

Switch database

Simply modify the configuration file and switch to other databases. Use MySql as follows:

ConnectionString= "server=localhost; user id=root; password=*; database=northwind" providerName= "MySql.Data.MySqlClient" / > connectionString= "server=localhost; user id=root; password=*; database=northwind" providerName= "MySql.Data.MySqlClient" / >

(MySql Connector Net must be installed to run)

ADO.NET provides database-independent classes and simplifies the development of database-independent applications.

However, there are still a lot of things to pay attention to in developing database-independent applications, such as the above-mentioned problem of count (*) return value types, and the need to write database-independent Sql, which is not easy.

By the way, database irrelevance is also limited, and the way the editor introduces it is only applicable to relational data.

The above is the example of the ADO.NET database independent application that Xiaobian shared with you. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report