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

What are the foundations of ADO.NET?

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

Share

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

This article mainly explains "what are the basics of ADO.NET". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the basics of ADO.NET.

Although we all know that ADO.NET is an operation on a database, it is not easy to really say what ADO.NET means.

ADO.NET, which stands for ActiveX Data Objects, is a library of COM components used to access data in microsoft technology. The reason why it is called ADO.NET should be advertised by Microsoft itself, hoping to give priority to the use of this data access interface in the NET programming environment. The above paragraph basically comes from Baidu encyclopedia. To put it simply, ADO.NET is a kind of data access interface, which allows us to call the corresponding class library in the program to add, delete, modify and query the database (usually SQL Server, access and other databases).

Several major components of ADO.NET

ADO.NET consists of five class libraries, which are:

Connection (used to establish a connection to the database)

Command (for executing SQL statements)

DataReader (for reading data)

DataAdapter (used to populate data into DataSet)

DataSet (dataset, for use in programs)

In general, the way to access a database from a program is:

Create a connection to the database

Open a database connection

Create an ADO recordset

Extract the required data from the recordset

Close the recordset

Close the connection

The following is explained according to this process combined with the five class libraries of ADO.NET.

To use ADO.NET, you need to reference System.Data.SqlClient in your program. It contains data access classes that operate on Sql Server:

SqlConnection: connecting to the database

SqlCommand: database named objects

SqlCommandBuilder: generating SQL command

SqlDataReader: data reader

SqlDataAdapter: data adapter for populating DataSet

SqlParameter: define parameters for stored procedures

SqlTransaction: database transaction

Establish a connection

First of all, to access the database, we need a medium to connect the program to the database. This is the connection string, and its basic syntax is: Data Source (data source) + Initial Catalog (database name) + User ID (user name) + Password (password).

The copy code is as follows:

String connectString = "Data Source = myServerAddress;Initial Catalog = myDataBase;User Id = myUserName; Password = myPassword;"

Or

The copy code is as follows:

String connectString = "Server = myServerAddress;Database = myDataBase; User Id = myUsername; Password = myPassword;"

Note: for Sql Server, it supports two authentication methods, one is windows authentication and the other is Sql Server authentication. If you want to use windows authentication, you need to include the Integrated Security attribute in the connection string. This property defaults to False. Need to be set to True before you can use windows authentication.

In addition to these necessary fields, there are many optional attributes in the connection string. I will not enumerate one by one here, listing some relevant information for interested friends to consult, and which attributes a connection string can contain (https://www.jb51.net/article/67742.htm).

Then, with the connection string, you can create the connection object.

SqlConnection connection = new SqlConnection (connecString)

Or you can use a special connection string generator:

The copy code is as follows:

SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder ()

{

DataSource= ""

InitialCatalog= ""

UserID= ""

Password= ""

}

SqlConnection connection = new SqlConnection (connectionStringBuilder.ToString ())

You can then use the connection object to open or close the connection.

Connection.Open ()

Connection.Close ()

Execute a command

After opening the connection, you can manipulate the database, where you need to use the SqlCommand command object.

It has four main properties that are assigned default values at initialization time:

CommandText: empty string ("")

CommandTimeout:30

CommandType:CommandType.Text

Connection:Null

Create a command object:

SqlCommand command = connection.CreateCommand ()

Or

SqlCommand command = new SqlCommand ()

SqlCommand contains several important attributes:

CommandText: a SQL statement, indication, or stored procedure used to get or set the row of a drug to a data source.

CommandType: sets the type of SQL statement you execute. There are three enumerations: Text (SQL text command), StoredProcedure (stored procedure), and TableDirect (table name).

Parameters: set the parameters you need in your T-SQL.

There are several important ways:

ExecuteNonQuery: returns the number of rows affected by the execution of the SQL statement (int), mainly performing add, delete and modify operations.

ExecuteReader: executes a SQL or stored procedure that returns a SqlDataReader type, which is mainly used for query.

ExecuteScalar: returns the first row and first column of the execution result set, or NULL if there is no data.

CreateParameter: create a SqlParameter instance.

Examples are as follows:

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;// must using System.Data.SqlClient;// must namespace Command {class Program {static void Main (string [] args) {SqlConnectionStringBuilder conStr = new SqlConnectionStringBuilder (); conSt.DataSource=@ ".\ SQLEXPRESS"; conStr.IntegratedSecurity=true; conStr.InitialCatalog= "db_Test"; StringBuilder strSQL = new StringBuilder (); for (int iTuno [)

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