The 14th Chapter uses ADO to query and manipulate data

Source: Internet
Author: User

I. Appending and deleting strings using the StringBuilder class
1. Create an object of the StringBuilder class StringBuilder sb=new StringBuilder ("Initial string value"); The 2.Append () method stitching the string sb.    Append ("hehe"); The result is: initial string value hehe 3. Insert the string SB at the specified location using the Insert () method.    Insert (position, string); Sb.    Insert (2, "on"); The result is: Initialize the string value Oh 4. Use the Remove () method to remove the string sb. Remove (start position, delete length) sb.    Remove (7,2); The result is: Initialize the string value
Two. Datarader object read Data 1.HasRows property: Determine whether to read data, if there is data is true, and vice versa is False 2.Read () method: forward to the next row read Data 3.Close () method: Close DataReader Object
Case: StringBuilder sb = new StringBuilder ();
SelectCount (*) fromstudent
Sb. Appendline ("select");
Sb. Appendline ("[Studentno]");
Sb. Appendline (", [Studentname]");
Sb. Appendline ("from");
Sb. Appendline ("Student");
SqlCommand com = new SqlCommand (sb.) ToString (), con);
SqlDataReader dr=com. ExecuteReader ();
Determines whether the DataReader object returns a result, and if there is a return result hasrows the value is true, the loop reads
if (Dr. HasRows)
{
while (Dr. Read ())
{
Console.WriteLine ("Name:" + dr["studentname"] + "\ T Study number:" + dr["Studentno"]);
}
}
Close the DataReader object
Dr.        Close (); Three. Use the Excutenonquery () method of the Command object to manipulate data excutenonquery () is primarily used for adding changes to data and deleting cases: Adding grade information StringBuilder SB = new Stringbuil Der ();
Sb. Appendline ("INSERT into");
Sb. Appendline ("Grade");
Sb. Appendline ("([Gradename])");
Sb. Appendline ("values");
Sb. Appendline ("('" +gradename+ "')");
SqlCommand com = new SqlCommand (sb.) ToString (), con);
int count=com. ExecuteNonQuery ();

The 14th Chapter uses ADO to query and manipulate data

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.