ADO.NET basic summary and example introduction

Keywords: Database SQL xml

1, When it comes to ADO.NET, it is often confused with ASP.NET. There is a big difference between the two. There is no comparability. Here are the differences between the two before:

**ADO.NET is a set of object-oriented class libraries used to interact with data sources (databases). * *

1. ASP.NET is a class library (Technology) provided by Microsoft in. net framework for developing Web applications. It is encapsulated in System.Web.dll, and the corresponding program is the namespace system.web. 2. ADO.NET provides consistent access to Microsoft Sql Server, XML and other data sources as well as data sources exposed through OLE DB and XML. ado.net class is in system.data.dll and integrated with XML class in System.XML.dll. 3. ado.net is a kind of access way of database, which can not be compared with asp.net. Asp.net is a website development technology based on. Netframework. ADO.NET is mainly used for database access

In a word, ADO.NET is used to deal with database access
Way.

Here are five common objects of ADO.NET:

  • Connection class (connection object)

>To interact with the database, you must connect to it. The Connection help indicates the database server, database name, user name, password and other parameters needed to connect to the database. The Connection object will be used by the Command object, so that you can know which data source to execute the Sql Command (script).
The process of interacting with a database means that you have to indicate what to do. This is performed by the Command object. Developers use the Command object to send SQL statements to the database.

using (SqlConnection conn = new SqlConnection("data source=10.10.198.111;Database=systemconfig;uid=sa;password=sa"))
            {
                conn.Open();
            }

The string in the SqlConnection parameter is the address, name, user name, password and other information of the connection database. It is usually placed in the web.config configuration file. When it needs to be called, the configuration information is directly referenced. The code is as follows:

 <!--Database connection address-->
  <connectionStrings>
    <add name="connstr" connectionString="data source=10.10.198.111;Database=systemconfig;uid=sa;password=sa"/>
  </connectionStrings>
private static string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
  • Command object

After successfully establishing a connection with the database, you can use the Command object to execute commands such as add, delete, modify and query. The common methods of the Command object are: ExecuteReader() method, ExecuteScalar() method, ExecutNonQuery() method

 using (SqlConnection conn=new SqlConnection(connstr))
            {
                using (SqlCommand cmd = new SqlCommand(str, conn))
                {
                    conn.Open();
                    int r=cmd.ExecuteNonQuery();
                }
            }
  • DataAdapter class

The data used by some developers is mainly read-only, and developers rarely need to change it to the underlying data source. In the same way, it is required to cache data in memory to reduce the number of times that unchanged data is called by the database. DataAdapter object is a kind of object used to act as a bridge between dataset object and actual data source. As long as dataset object has DataAdapter object, its working steps are: one is to execute sql statement through command object, retrieve data from data source, and fill the retrieved results into dataset object. The other is to write the user's changes to the dataset object to the data source.

 using (SqlConnection cnn = new SqlConnection(connstr))
            {
                cnn.Open();
                using (SqlCommand cmd = cnn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    SqlDataAdapter apter = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    apter.Fill(ds);
                    return ds.Tables[0];
                }
            }
  • Class DataSet

A dataset object is a representation of data in memory. It includes multiple DataTable objects, and DataTable contains rows and columns, just like a table in a common database. Dataset is used in a specific scenario - to help manage data in memory and support data disconnection.

  • Class DataTbale

DataTbale is a grid virtual table that holds data temporarily (a table that represents data in memory). DataTable is the core object in ADO DOT NET Library

The extension DataGridView multiline edit deletion code is as follows:

   //Update datatable
        /// <summary>
        ///Update DataTable
        /// </summary>
        /// <param name="table">DataTable</param>
        /// <returns></returns>
        public static bool UpdatDataTables(DataTable table)
        {
            //sql statement is to get table structure
            string sql = "SELECT * FROM " + table.TableName.ToString() + "";
            SqlConnection con = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            using (SqlDataAdapter adapter = new SqlDataAdapter())
            {
                adapter.SelectCommand = cmd;//Add command object before connection object
                adapter.SelectCommand.CommandText = sql;
                adapter.SelectCommand.CommandType = CommandType.Text;
                adapter.SelectCommand.Connection = con;
                adapter.SelectCommand.Connection.ConnectionString = Constr;
                adapter.SelectCommand.Connection.Open();
                SqlCommandBuilder cb = new SqlCommandBuilder(adapter);//Adapter
                int r = adapter.Update(table);
                //int r = adapter.Update(table);
                return r > 0;
            }
        }
        //delete
        /// <summary>
        ///Delete data
        /// </summary>
        ///< param name = "strip" > Student ID to delete < / param >
        /// <returns></returns>
        public static bool DeleteOneData(string strid)
        {
            #region Stitching deletion
            string sql = "DELETE students WHERE StuNo IN (" + strid + ")";
            using (SqlConnection con = new SqlConnection(Constr))
            {
                con.Open();
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    int s = cmd.ExecuteNonQuery();
                    return s > 0;
                }
            }
            #endregion

        }
        public static int DeleteXunhuan(string stuno)
        {
            string sql = "DELETE students WHERE StuNo='" + stuno + "'";
            using (SqlConnection con = new SqlConnection(Constr))
            {
                con.Open();
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    int r = cmd.ExecuteNonQuery();
                    return r;
                }
            }
        }
        //Click Modify
        /// <summary>
        ///Modify data
        /// </summary>
        ///< param name = "stuNo" > student number < / param >
        ///< param name = "stunne" > name < / param >
        ///< param name = "stuex" > gender < / param >
        ///< param name = "stuage" > age < / param >
        ///< param name = "classno" > class number < / param >
        /// <returns></returns>
        public static bool UpdteEndWrite(string stuno, string stuname, string stusex, int stuage, string classno)
        {
            string sql = "UPDATE students SET StuName='" + stuname + "' ,Sex='" + stusex + "' ,Age=" + stuage + " ,ClassNo='" + classno + "' WHERE StuNo='" + stuno+ "'";
            using (SqlConnection con = new SqlConnection(Constr))
            {
                con.Open();

                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    int r = cmd.ExecuteNonQuery();
                    return r>0; 
                }
            }
        }
Published 8 original articles, won praise 2, visited 141
Private letter follow

Posted by feest on Tue, 21 Jan 2020 06:13:06 -0800