How does asp.net read characters from C # strings?

Source: Internet
Author: User

Asp.net reads character-defined strings from C # strings and converts them to character arrays. Then, you can use the appropriate StringReader. Read method to Read the character arrays as needed.

Asp tutorial. net how to read characters from c # strings
Define a string and convert it to a character array. Then, you can use the appropriate stringreader. read method to read the string array as needed.
[C #]

Using system;
Using system. io;
Public class charsfromstr
{
Public static void main (string [] args)
{
// Create a string to read characters from.
String str = "some number of characters ";
// Size the array to hold all the characters of the string,
// So that they are all accessible.
Char [] B = new char [24];
// Create a stringreader and attach it to the string.
Stringreader sr = new stringreader (str );
// Read 13 characters from the array that holds the string, starting
// From the first array member.
Sr. read (B, 0, 13 );
// Display the output.
Console. writeline (B );
// Close the stringreader.
Sr. close ();
}
}

The example allows you to read a certain number of characters from a specified position in an existing string. Use stringreader to complete this operation

In asp.net tutorial 2.0, a new declarative expression syntax is used to parse a connection string value at runtime, referencing the database tutorial connection string by name. The connection string is stored under the <connectionstrings> Configuration section in the web. config file to facilitate maintenance of all pages in the application at a single location.

The sample code is as follows:

<? Xml version = "1.0"?>
<Configuration>
<Connectionstrings>
<Add name = "pubs" connectionstring = "server = localhost;
Integrated security = true; database = pubs; persist security info = true"
Providername = "system. data. sqlclient"/>
<Add name = "northwind" connectionstring = "server = localhost;
Integrated security = true; database = northwind; persist security info = true"
Providername = "system. data. sqlclient"/>
</Connectionstrings>
<System. web>
<Pages stylesheettheme = "default"/>
</System. web>
</Configuration>

  

Program Code Description: In the program code of the preceding example. under the <connectionstrings> Configuration node in the config file, two database connection strings are set, pointing to the pubs and northwind sample databases respectively. Note that a data source control is introduced in 2.0, such as the sqldatasource control. We can set the connectionstring attribute of the sqldatasource control to an expression <% $ connectionstrings: pubs %>, the expression is parsed as a connection string by the asp.net Analyzer at runtime. You can also specify an expression for the providername attribute of sqldatasource, for example, <% $ connectionstrings: pubs. providername %>. The specific usage and new features will be detailed in the subsequent sections. Now you have a basic understanding.

Of course, we can also use the following method to directly read the database connection string from the configuration file. First, we need to reference the using system. web. configuration namespace, which contains classes used to set asp.net configurations.

String connectionstring = configurationmanager. connectionstrings ["northwind"]. connectionstring;

Program Code Description: In the program code of the preceding example, we can use connectionstrings ["northwind"] to read the corresponding northwind string. Similarly, you can use connectionstrings ["pubs"] to read the corresponding pubs string.

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.