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

How to access ADO.NET data

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to access ADO.NET data". In daily operation, I believe many people have doubts about how to access ADO.NET data. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to access ADO.NET data"! Next, please follow the editor to study!

When we insert, delete, update, and read data from a database, we often need to write a lot of the same or similar code, which looks bloated. Not only is it a waste of time to write code like this, but the structure of the program looks unclear and disorganized. There are many classes for database operations in System.Data.SqlClient.

Access to data sources in ADO.NET is controlled by managed providers. Although there are two major differences between managed providers and OLE DB, they are very similar. First, access the ADO.NET managed provider to run in the .NET environment, retrieve and display data through DataReader and DataTable .NET classes. Second, their architectures are relatively simple because they are optimized to adapt to .NET.

At this point, ADO.NET is divided into two different types of managed providers: one for SQL Server 7.0 or later, and the other for all OLE DB providers that you may have installed to access ADO.NET. Although the classes used in the two managed providers are different, they both follow similar naming patterns. Except for the prefix, all other names are the same. The former case is prefixed with SQL, and the latter is prefixed with ADO.

You need to use the SQL class to access the SQL Server table, because the SQL class skips the middle tier rendered by the OLE DB provider and goes directly to the internal API of the database server. The ADO class is the .NET interface at the top of the OLE DB provider that uses the COM Interop bridge to work.

So we can use the class that has been provided, write our own class, visit ADO.NET and write some functions in the class we have written. These functions are frequently used in the program, and the functions can take different parameter lists. Here is how I write a simple class myself. Several of its functions can be seen in a large number of programs.

Using System.Data.SqlClient; class DataLev {private SqlCommand myCmd; private SqlConnection sqlConn; private SqlDataAdapter myAdp; public SqlConnection getConn () {/ / returns the database connection string string constr = @ "Data Source = localhost; Initial Catalog = CustomersManage; Integrated Security= true"; sqlConn = new SqlConnection (constr) Return sqlConn;} public void ExcuteCmd (string sqlCmd) {/ / execute the general SQL statement (select,insert,delete,update) sqlConn = this.getConn (); sqlConn.Open (); myCmd = new SqlCommand (sqlCmd, sqlConn) MyCmd.ExecuteNonQuery (); / / returns the affected row myCmd.Dispose (); sqlConn.Close ();} at this point, the study on "how to access ADO.NET data" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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