Detailed introduction to the non-connection mode of ADO. NET

Source: Internet
Author: User

No connection mode: You can operate on data in the memory when the connection is not enabled. DataAdapter provides services for the connectionless mode through the Management connection. When you want to query data from the database, DataAdapter opens a connection, fill in the specified DataSet and close the connection immediately after the data is read. Then, you can modify the data and use DataAdapter to open the connection again for persistent modification (whether update, delete, or update ), finally, the connection is automatically closed. When no connection mode is used, some independent data will not be changed or rarely changed, because the actual data in the database may change during the time when the DataSet is filled and the data is updated. If you need to persist the data to the database immediately, use the connection mode.

Read data to DataSet: No connection means that a connection establishes a session with the database. The requested data is read into the DataSet, and the session is closed by disconnecting the database, in this case, the session is closed because it is disconnected from the database, and DataSet becomes a connectionless database.

Copy codeThe Code is as follows:
/// <Summary>
/// Query Student Information
/// </Summary>
/// <Returns> return the DataSet filled with the student table </returns>
Public DataSet GetUserInfor ()
{
String str = "Data Source =.; Initial Catalog = Student; Integrated Security = True ";
Var conn = new SqlConnection (str );
DataSet ds = new DataSet ();
Var sda = new SqlDataAdapter ("SELECT * FROM Student", conn );
Sda. Fill (ds, "student"); // when the fill method is called, SqlDataAdapter automatically opens the connection, reads data, and closes the connection.
Foreach (DataRow dr in ds. Tables ["student"]. Rows)
{
Console. WriteLine (dr ["name"]);
}
Return ds;
}

Save modifications to DataSet to the database
Insert data

Copy codeThe Code is as follows:
/// <Summary>
/// Insert student information and return the inserted DataSet
/// </Summary>
/// <Param name = "stu"> Student Entity class </param>
Public DataSet InsertStudnt (Student stu)
{
DataSet ds = GetUserInfor ();
String str = "Data Source =.; Initial Catalog = Student; Integrated Security = True ";
String SQL = "INSERT INTO student VALUES (@ name, @ age )";
Var conn = new SqlConnection (str );
Var cmd = new SqlCommand (SQL, conn );
Var sda = new SqlDataAdapter ();
SqlParameter sqlParam1 = new SqlParameter ()
{
ParameterName = "@ name ",
SourceColumn = "name"
};
SqlParameter sqlParam2 = new SqlParameter ()
{
ParameterName = "@ age ",
SourceColumn = "age"
};
SqlParameter [] sqlParamArray = new SqlParameter [] {sqlParam1, sqlParam2 };
Cmd. Parameters. AddRange (sqlParamArray );
Sda. InsertCommand = cmd;
DataRow dr = ds. Tables ["student"]. NewRow ();
Dr ["name"] = stu. name;
Dr ["age"] = stu. age;
Ds. Tables ["student"]. Rows. Add (dr );
Sda. Update (ds, "student ");
Return ds;

}

Update Data

Copy codeThe Code is as follows:
/// <Summary>
/// Update the name and age based on the ID
/// </Summary>
/// <Param name = "name"> name </param>
/// <Param name = "age"> age </param>
/// <Param name = "id"> Student ID </param>
/// <Returns> return the updated DataSet </returns>
Public DataSet UpdateStudent (Student stu, int id)
{
DataSet ds = GetUserInfor ();
String str = "Data Source =.; Initial Catalog = Student; Integrated Security = True ";
String SQL = "UPDATE student SET name = @ name, age = @ age WHERE id = @ id ";
Var conn = new SqlConnection (str );
Var cmd = new SqlCommand (SQL, conn );
Var sda = new SqlDataAdapter ();
SqlParameter param1 = new SqlParameter ()
{
ParameterName = "@ name", SourceColumn = "name"
};
SqlParameter param2 = new SqlParameter ()
{
ParameterName = "@ age ",
SourceColumn = "age ",
SqlDbType = SqlDbType. Int
};
SqlParameter param3 = new SqlParameter ()
{
ParameterName = "@ id ",
SourceColumn = "id"
};
SqlParameter [] param = new SqlParameter [] {param1, param2, param3 };
Cmd. Parameters. AddRange (param );
Sda. UpdateCommand = cmd;
DataTable dt = ds. Tables ["student"];
Foreach (DataRow dr in dt. Rows)
{
Int oldID = Convert. ToInt32 (dr ["id"]);
If (oldID = id)
{
Dr ["name"] = stu. name;
Dr ["age"] = stu. age;
}
}
Sda. Update (ds, "student ");
Return ds;
}

Delete data

Copy codeThe Code is as follows:
/// <Summary>
/// Delete a student by ID
/// </Summary>
/// <Param name = "id"> return the updated DataSet </param>
Public DataSet DeleteStudent (int id)
{
DataSet ds = GetUserInfor ();
String str = "Data Source =.; Initial Catalog = Student; Integrated Security = True ";
String SQL = "DELETE FROM student WHERE id = @ id ";
Var conn = new SqlConnection (str );
Var cmd = new SqlCommand (SQL, conn );
Var sda = new SqlDataAdapter ();
SqlParameter param = new SqlParameter ()
{
ParameterName = "@ id", SourceColumn = "id", SqlDbType = SqlDbType. Int
};
Cmd. Parameters. Add (param );
Sda. DeleteCommand = cmd;
DataTable dt = ds. Tables ["student"];
Foreach (DataRow dr in dt. Rows)
{
Int oldId = Convert. ToInt32 (dr ["id"]);
If (oldId = id)
Dr. Delete ();
}
Sda. Update (ds, "student ");
Return ds;
}

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.