Ado. NET 9-non-connected mode, in-memory database dataset,datatable---Shinepans

Source: Internet
Author: User

Datasets are referred to as datasets, which can be compared to in-memory databases, the dataset is the core of ADO, and the core object of the Distributed data scheme is to implement the core component of the data query based on the non-connection.

DataTable Common Properties:

Property Description
Columns Gets the collection of columns that belong to the table
Rows Gets the collection of rows that belong to the table
TableName Gets or sets the name of the DataTable

DataTable Method:

Method Description
AcceptChanges Commits all changes made to the table since the last call to AcceptChanges
Clear Clears all data in the DataTable
NewRow Create a new DataRow with the same schema as the 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 database DataSet ds = new DataSet (); Create database DataSet object DataTable dt = new DataTable (); Create the Database DataTable object ds. Tables.add (DT); Adds a data table object to the DataSet object collection in dt.  Columns.Add ("name", typeof (String)); Add a column dt to the DataTable. Columns.Add ("Address", typeof (String));//Add a column to the DataTable, DataRow dr = dt.   NewRow ();            Get data table, Row object dr[0] = "Pimpin";  DR[1] = "Wuhan"; Adds data to the row object dt.            Rows.Add (DR); Dt. Rows.Add (new object[] {"Painpampan", "Beijing"}); Adds a new object to the data row, foreach (DataRow v in dt.            Rows) {Console.WriteLine ("{0} {1}", V[0], v[1]); } String connection = "server= Pinchamp \\sqlexpress;database=db_test; Trusted_connection=true ";        SqlConnection sc = new SqlConnection (connection); Sc.            ConnectionString = connection; try {sc.  Open (); Open the Database connection Console.WriteLine ("Already Open database connection!"); /start:5. Exporting records in the database///////////////////////////////////////////////////////////* SqlCommand cmd = new Sqlcomm                and ("SELECT * from Db_student", SC); SqlDataReader SDR = cmd. ExecuteReader (); Executes the Find record command while (SDR.                Read ()) {Console.WriteLine ("{0}{1}{2}{3}", Sdr[0], sdr[1], sdr[2], sdr[3]); *///end:5. Export the records in the database//////////////////////////////////////////////////////////////start:4. Querying database Records///////////// /* SqlCommand cmd = new SqlCommand ("SELECT count (*) from D                B_student ", SC); int i = (int) cmd.  ExecuteScalar ();//The command Console.WriteLine to perform a lookup record ("{0} data in the table", i.ToString ()); *///end:4. Querying database records//////////////////////////////////////////////////////////////////start:3. Modifying the code for database data////////////////// /* SqlCommand cmd = new SqlCommand ("UPDATE db_student SET Student_gra  de=99 where [email protected] ", SC); Create the SqlCommand object cmd. Parameters.Add ("@name", SqlDbType.VarChar).                Value = "Pan"; int i = cmd.                ExecuteNonQuery ();   if (i > 0) Console.WriteLine ("Modified successfully!"); *///end:3. Code///////////////////////////////////////////////////////////start:1 to modify database data. Delete the database record code snippet///////////// /* String cmdtext = "DELETE from db_student WHERE [Email pro                Tected] ";                SqlCommand cmd = new SqlCommand (Cmdtext, SC); Cmd. Parameters.Add ("@name", SqlDbType.VarChar).                Value = "Pan"; int i = cmd.                ExecuteNonQuery (); if (i > 0) Console.WriteLine ("Delete record successfully!"); *///end:1. Deleting a database record code snippet/////////////////////////Start:2. Adding code to a record///////////////////////////////////////////////////////////// /* SqlCommand cmd = new SqlCommand ();//Create SqlCommand object cmd. CommandType = CommandType.Text; Sets the Execute text command cmd. Connection = SC; Sets the object property of CMD. CommandText = "INSERT into Db_student (student_name,student_age,student_address,student_grade) VALUES (@n                Ame, @age, @address, @grade) "; Add a parameter and assign a value cmd 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 database Add record command if (i > 0) Console.WriteLine ("Add record Success");  */////console output add record//end:2. Add the code for the record/////////////////////////////////////////////////////////////////          } catch (Exception ex) {Console.WriteLine ("Open database error: {0}", ex.            Message); } finally {SC.                Close ();            Console.WriteLine ("Database connection closed!");        } System.Console.ReadLine (); }    }}


Results:



Data is processed in memory before the database connection is opened

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.