Querying and manipulating data using ADO

Source: Internet
Author: User

I. Appending and deleting strings using the StringBuilder class


1. Creating an object of the StringBuilder class

StringBuilder sb=new StringBuilder ("Initial string value");

2.Append () Method stitching strings

Sb. Append ("hehe");

The result is: initial string value hehe

3. Insert a string at the specified location using the Insert () method

Sb. Insert (position, string);

Sb. Insert (2, "on");

The result is: Initialize the string value hehe

4. Use the Remove () method to delete a 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 properties: Determine if the data is read, true if there is data, false instead

2.Read () Method: Advances to the next row of read data

3.Close () Method: Close the 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. Manipulating data using the Excutenonquery () method of the Command object

Excutenonquery () is primarily used to modify and delete data

Case: Adding Grade information

StringBuilder sb = new StringBuilder ();
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 ();

Querying and manipulating data using ADO

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.