asp.net 如何從C# 字串中讀取字元

來源:互聯網
上載者:User

asp教程.net 如何從c# 字串中讀取字元
定義字串並將其轉換為字元數組,然後,可以使用適當的 stringreader.read 方法按需要讀取該字元數組。
[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();
  }
}

樣本允許您在現有字串中從指定的位置開始讀取一定數目的字元。使用 stringreader 完成此操作

asp.net教程 2.0中,使用了一種在運行時解析為連接字串值的新的聲明性運算式文法,按名稱引用資料庫教程連接字串。連接字串本身儲存在 web.config 檔案中的 <connectionstrings> 配置節下面,以便易於在單個位置為應用程式中的所有頁進行維護。

  範常式序代碼如下:

<?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>

  

程式碼說明:在上述範例的程式碼中,我們在web.config檔案中的<connectionstrings> 配置節點下面設定了兩個資料庫連接字串,分別指向pubs和northwind兩個樣本資料庫。注意,在2.0中引進了資料來源控制項,例如sqldatasource 控制項,我們可以將sqldatasource 控制項的 connectionstring 屬性被設定為運算式 <%$ connectionstrings:pubs %>,該運算式在運行時由 asp.net 分析器解析為連接字串。還可以為sqldatasource 的 providername 屬性指定一個運算式,例如 <%$ connectionstrings:pubs.providername %>。其具體的用法和新特徵將在以後的章節進行詳細的介紹。現在有個基礎的瞭解即可。

  當然,我們也可以用下面的方式從設定檔直接讀取資料庫連接字串。首先我們需要引用using system.web.configuration命名空間,該命名空間包含用於設定 asp.net 配置的類。

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

  程式碼說明:在上述範例的程式碼中,我們可以利用connectionstrings["northwind"]讀取相應的northwind字串。同理以可以利用connectionstrings["pubs"]讀取相應的pubs字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.