9-non-connection mode of ADO. NET, DataSet in memory, DataTable --- ShinePans,

Source: Internet
Author: User

9-non-connection mode of ADO. NET, DataSet in memory, DataTable --- ShinePans,

DataSet is called a DataSet. It can be compared to a database in memory. DataSet is ADO. NET core, supports ADO.. NET disconnected. The core object of the distributed data solution is also the core component for non-connection-based data query.

Common attributes of DataTable:

Attribute Description
Columns Obtains the set of columns in the table.
Rows Obtains the set of rows in the table.
TableName Get or set the DataTable name

DataTable method:

Method Description
AcceptChanges Submit all changes made to the table since the last AcceptChanges call
Clear Clear all data in the DataTable
NewRow Create a new DataRow with the same schema as this table

Using System; using System. collections. generic; using System. data; using System. data. sqlClient; using System. linq; using System. text; using System. threading. tasks; namespace SQLTest {class Program {static void Main (string [] args) {// connect to the database DataSet ds = new DataSet (); // create a database DataSet object DataTable dt = new DataTable (); // create a database DataTable object ds. tables. add (dt); // Add the data table object to the dt in the DataSet object set. columns. add ("name", typeof (st Ring); // Add the column dt to the able. columns. add ("address", typeof (string); // Add the column DataRow dr = dt to the able. newRow (); // obtain the data table. The row object dr [0] = "pan"; dr [1] = "Wuhan"; // Add the data dt to the row object. rows. add (dr); dt. rows. add (new object [] {"pan", "Beijing"}); // Add a new object foreach (DataRow v in dt. rows) {Console. writeLine ("{0} {1}", v [0], v [1]);} string connection = "server = Pan Shang \ SQLEXPRESS; database = db_test; trusted_Connection = true "; Sq LConnection SC = new SqlConnection (connection); // SC. ConnectionString = connection; try {SC. Open (); // Open the database connection Console. WriteLine ("the database connection has been enabled! "); // START: 5. export records in the database //////////////////////////////////// /// // * SqlCommand cmd = new SqlCommand ("SELECT * FROM db_student ", SC); SqlDataReader sdr = cmd. executeReader (); // execute the query record command while (sdr. read () {Console. writeLine ("{0} {1} {2} {3}", sdr [0], sdr [1], sdr [2], sdr [3]);} * // END: 5. export records in the database //////////////////////////////////// /// // START: 4. query database records /////////////// //////////////////////////////////////// //// // * SqlCommand cmd = new SqlCommand ("SELECT count (*) FROM db_student ", SC); int I = (int) cmd. executeScalar (); // execute the command Console for searching records. writeLine ("A total of {0} pieces of data in the table", I. toString (); * // END: 4. query database records ///////////////////////////////////// /// // START: 3. modify the database data code /////////////////////////////////// /// // * SqlCommand cmd = new SQL Command ("UPDATE db_student SET student_grade = 99 where student_name = @ name", SC); // create the SqlCommand object cmd. parameters. add ("@ name", SqlDbType. varChar ). value = "pan"; int I = cmd. executeNonQuery (); if (I> 0) Console. writeLine ("modification successful! "); * // END: 3. modify the database data code /////////////////////////////////// /// // START: 1. delete the database record code segment //////////////////////////////////// //// // * string plain text = "delete from db_student WHERE student_name = @ name "; sqlCommand cmd = new SqlCommand (plain text, SC); cmd. parameters. add ("@ name", SqlDbType. varChar ). value = "pan"; int I = cmd. executeNonQuery (); if (I> 0) Console. writeLine ("the record is deleted successfully.! "); * // END: 1. delete the database record code segment //////////////////////////////////// /// // START: 2. add the recorded code //////////////////////////////////// //// // * SqlCommand cmd = new SqlCommand (); // create the SqlCommand object cmd. commandType = CommandType. text; // set the command to run the command cmd. connection = SC; // set the object property cmd. commandText = "insert into db_student (student_name, student_age, student_address, student_grade) VALUES (@ nam E, @ age, @ address, @ grade) "; // Add a parameter and assign the cmd value to the parameter. parameters. add ("@ name", SqlDbType. varChar, 10 ). value = "pan"; cmd. parameters. add ("@ age", SqlDbType. int ). value = 19; cmd. parameters. add ("@ address", SqlDbType. varChar ). value = "Wuhan"; cmd. parameters. add ("@ grade", SqlDbType. int ). value = 100; int I = cmd. executeNonQuery (); // execute the database add record command if (I> 0) Console. writeLine ("successfully added the record"); * // The console outputs the Add record // END: 2. code for adding a record /////// //////////////////////////////////////// /// //} Catch (Exception ex) {Console. writeLine ("Opening Database Error: {0}", ex. message);} finally {SC. close (); Console. writeLine ("the database connection is closed! ") ;}System. Console. ReadLine ();}}}


Result:



Process data in memory before the database connection is enabled


In ADONET technology, the C # object can be connected to the SQL database.

Using System. Data;
Using System. Data. SqlClient;
..

String strConnection = "user id = sa; password = ;";
StrConnection + = "initial catalog = Northwind; Server = YourSQLServer ;";
StrConnection + = "Connect Timeout = 30 ";

SqlConnection objConnection = new SqlConnection (strConnection );
..

ObjConnection. Open ();
ObjConnection. Close ();

The strConnection variable stores the connection string required to connect to the database. It specifies the data provider to be used and the data source to be used.
First, the namespace used to connect to SQL Server is "System. Data. SqlClient ".
The second is his connection string. We will introduce them one by one (note: the parameters are separated by semicolons ):
"User id = sa": the authenticated user name used to connect to the database is sa. It also has an alias "uid", so we can also write this statement as "uid = sa ".
"Password =": The verification password for connecting to the database is blank. Its alias is "pwd", so we can write it as "pwd = ".
Note that your SQL Server must have a user name and password set to log on. Otherwise, you cannot log on using this method. if your SQL Server is set to Windows logon, you do not need to use the "user id" and "password" methods to log on here, you need to use "Trusted_Connection = SSPI" to log on.
"Initial catalog = Northwind": the data source used is "Northwind". Its alias is "Database". This statement can be written as "Database = Northwind ".
"Server = YourSQLServer": Use a Server named "YourSQLServer. its alias is "Data Source", "Address", "Addr ". if the local database is used and the Instance name is defined, it can be written as "Server = (local)/Instance name". If it is a remote Server) "Replace with the name or IP address of the remote server.
"Connect Timeout = 30": the connection Timeout is 30 seconds.

Here, the constructor used to create a connection object is SqlConnection.

Concepts of DATASET and able in ADONET

From the perspective of development ~ DataSet is a set of data.
DataTable is a table ~ That is, data
DataColumn is a column in the table.
DataRow is the number of data entries.

In plain words
DataSet is a folder
DataTable is a file
Both DataColumn and DataRow are the contents of this file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.