s1/c# Language and database Technology Fundamentals/15-using ADO to query and manipulate data

Source: Internet
Author: User
Tags odbc string format

String and StringBuilder

There is often a problem when using the String class: When you reassign a value for the same string each time, a new string object is created in memory, and the new object needs to be allocated new space, which increases the overhead of the system. Because system. The string class is an immutable data type, and once the string object is initialized, the value of the string object cannot be changed. When a change is made to the value of the string, a new string object is actually created. Now let's analyze what the output of this piece of code consists of the following three statements.

String strText = "Hello";

StrText + = "World";

Console.WriteLine (StrText);

The answer is simple, and it is easy to see that the output of the above code is "HelloWorld". But how many objects does this piece of code create? Let's illustrate it in a graphical way. When you execute the first statement, you first create a string class object with a value of "Hello", and then assign the object's reference to strtext through the assignment operator.

When you execute the second statement, strtext updates the value on the surface. But in fact, two new objects were created in memory, the values of which are "world" and "HelloWorld". At this point, there are three objects in memory, namely Hello, world, and HelloWorld. and Strtest is referring to the HelloWorld object. If you have a string object in your program that repeatedly modifies the content, the overhead is costly and may result in a decrease in the performance of your application. How to avoid this situation? Is there a class that can be repeatedly modified without having to create new objects? Yes, this is the StringBuilder class in C #.

Next, we introduce the definition and processing method of StringBuilder class object.

To address the above problem, Microsoft provides a System.Text.StringBuilder class that represents a mutable string. Although the StringBuilder class does not want the string class to have many methods of handling strings, the StringBuilder class object executes much faster than a string class object when replacing, adding, or removing strings. Now let's look at how the StringBuilder class object is defined.

Grammar:

Declares an empty StringBuilder object

StringBuilder Object name = new StringBuilder ();

Declares a StringBuilder object with a value of "string initial value"

StringBuilder Object name = new StringBuilder ("string initial value");

For example

System.Text.StringBuilder sbtext = new StringBuilder ();

Sbtext.append ("Hello");

Sbtext.append ("HelloWorld");

In the above code, use the StringBuilder class to refer to the System.Text namespace first, and then create the StringBuilder object Sbtext. When the Sbtext object calls the Append () method, appends a new string to the original string of the Sbtext object instead of creating a new object. The StringBuilder class is a dynamically allocated space that allows you to expand the number of characters in the string it encapsulates. If necessary, you can use the ToString () method to convert the value of the Sbtext object to a string type output.

StringBuilder commonly used properties and methods

Property

Description

Capacity

Gets or sets the maximum number of characters that can be included in the memory allocated by the current object

Length

Gets or sets the length of the current object

Method

Description

StringBuilder Append (String value)

Append at end

StringBuilder Append (String format,object arg0,object arg1)

Add a string of a specific format

StringBuilder Insert (int index,string value)

Inserts the specified string at the specified location

Remove (int startindex,int length)

Remove the specified string

    • The steps to pop up the text Visualizer dialog box in the program debug state are as follows.

Set breakpoints → start debugging → view values into the Watch Window → The small magnifying glass icon to the right of the value column in the stand-alone Watch Window pops up the Text Visualizer dialog box.

    • When writing SQL statements, add a pair of brackets [] between the table name and column name of the database to distinguish between specific strings that do not conform to the naming conventions, or to prevent the column names of database tables from conflicting with system keywords. As the assumption indicates that the SELECT,SQL statement is "SELECT * from select;" A syntax error will be generated. However, if you change to "select * from [select];", this is the correct SQL statement to execute normally.

Attention:

    • If you want to convert a StringBuilder class object to a String class object, the only way is to use the ToString () method.
    • The StringBuilder class does not always improve performance, and he basically uses it when working with multiple strings. If you are just connecting two strings, it would be nice to use the System.String class.

Querying data

In the previous chapter, we explained how to connect an application to a database through a Connection object and use the ExecuteScalar () method of the Command object to get a single value from the query database. In actual work, we may query the database to return multiple records, how to get a query to get multiple rows and columns of data values? We can use the Command object's ExecuteReader () method to return a DataReader object that can read multiple records from the database by DataReader.

DataReader Object

Ado. NET DataReader objects can retrieve read-only, forward-only streams of data from the data source, extracting only one record from the data source at a time. Using DataReader can improve the performance of your application and reduce system overhead. DataReader belongs to a. NET data provider, and each. NET data provider has a corresponding DataReader class.

. NET data provider and its DataReader class

. NET Data Provider

DataReader class

Name space

SQL data Provider

SqlDataReader

System.Data.SqlClient

OLE DB data Provider

OleDbDataReader

System.Data.OleDb

ODBC data Provider

OdbcDataReader

System.Data.Odbc

Oracle Data Provider

OracleDataReader

System.Data.OracleClient

s1/c# Language and database Technology Fundamentals/15-using ADO to query and manipulate data

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.