asp.net prepared for Interview (4)

ADO.NET


Different between DataReader and DataSet:

The DataReader object helps in retrieving the data from a database in a forward-only, read-only mode.

 string connectionString =
            @"Data Source=KIDD\KIDD;Initial Catalog=rec_ie;Integrated Security=True";

            // Provide the query string with a parameter placeholder.
            string queryString =
                "SELECT * from dbo.Country " + "where CountryID > @valPoint";

            // Specify the parameter value.
            int paramValue = 0;

            // Create and open the connection in a using block. This
            // ensures that all resources will be closed and disposed
            // when the code exits.
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                // Create the Command and Parameter objects.
                SqlCommand command = new SqlCommand(queryString, connection);
                command.Parameters.AddWithValue("@valPoint", paramValue);

                // Open the connection in a try/catch block. 
                // Create and execute the DataReader, writing the result
                // set to the console window.
                try
                {
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        Response.Write(reader[1].ToString());
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }


DataSet object, which always remains disconnected from the database and reduces the load on the database.

string connectionString =
            @"Data Source=KIDD\KIDD;Initial Catalog=rec_ie;Integrated Security=True";

            // Provide the query string with a parameter placeholder.
            string queryString =
                "SELECT * from dbo.Country " + "where CountryID > 0";

            SqlConnection thisConnection = new SqlConnection(connectionString);
            SqlDataAdapter thisAdapter = new SqlDataAdapter(queryString, thisConnection);
            DataSet thisDataset = new DataSet();
            thisAdapter.Fill(thisDataset,"Country");

            Response.Write(thisDataset.Tables["Country"].Rows[0][1]);

DataSet可以離線處理,前後滾動.DataReader不能離線處理,且是隻讀的向前的,不過速度明顯會很快

DataSet可以存儲數據庫各種對象的,比如表觸發器等,而DataReader只能存儲遊標記錄 

DataSet可以更新回原來的數據庫,DataReader不行;

DataSet可以FORWORD     PREVIUS,而DataReader只能FW;

****************************************************************************************************************

ASP.NET Page 的生命週期:

1,  page Request: 用戶請求一個頁面

2,Start: 設置各種page的properties,例如 request, response。 同時 判斷頁面是否爲 postback?

3,Initialization: 各種control及其uniqueID的properties被設置

4,   Load: 如果是postback,則control將從view state 和 control state 取回信息

5, Postback event handling: 回發事件處理

6,Rendering:呈現  呈現HTML

7,Unload:卸載 呈現HTML後 對象會被釋放


http://msdn.microsoft.com/en-us/library/ms178472.aspx

*************************************************************************************************************

判斷用戶所用的語言:

The information regarding a user's locale can be accessed by using the System.Web.UI.Page.Cultureproperty.

所有控件的父類是:

The System.Web.Ul.Control class is the parent class for all Web server controls.

**************************************************************************************************************

如何獲取web.config中的connectionstring

string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["rec_ieConnectionString"].ConnectionString;

*************************************************************************************************************

Managing State:   1, Client - Side state   2, Server-Side State


Client Side State:

view state  ASP.NET uses view state to track values in controls between page requests. You can also add your own custom values to the view state.

ViewState主要是用來存放和視圖有關的一些狀態。比如,在用戶註冊時用戶填寫了一大堆數據,提交頁面後系統返回了一個“用戶名重複”的出錯信息,此時先前用戶在頁面上填寫的一些註冊資料全部沒有了。用戶會是什麼感覺呢?我想大多數用戶會很惱火。ASP.NET通過ViewState自動保存控件的狀態。你可能也發現了,文本框中的數據在頁面提交後還是存在的。

同時,我們也可以利用ViewState來保存一些程序需要的數據。

ViewState肯定是不能跨頁面使用的,而且每個用戶訪問到的ViewState都是獨立的。此外,ViewState也沒有什麼生命週期的概念,頁面在ViewState就在,頁面關閉了ViewState就關閉了

View State is the best way to retain values between multiple requests for the same page


Control state  Control state allows you to persist information about a control that is not part of the view state. This is useful to custom control developers. If view state is disabled for a control or the page, the control state will still function. 

就算頁面或者控件的ViewState被關閉它(Control state )還能起作用,彌補了ViewState能被禁止的不足


hidden fields  Like view state, HTML hidden fields store data without displaying that data to the user’s browser. This data is presented back to the server and is available when the form is processed.

 一般來說我們是無權訪問客戶端的機器,hidden 解決了這個問題


Cookies  A cookie stores a value in the user’s browser. The browser sends this value with every page request to the same server. Cookies are the best way to store state data that must be available for multiple webpages on a website.

Cookie只能保存不超過4K的字符串


Query strings   A query string is a value that is stored at the end of a URL. These values are visible to the user through his or her browser’s address bar. Use query strings when you want a user to be able to use email or instant messaging to store state data within a URL.

使用URL進行傳遞數據


Session、Application和Cache都是保存在服務器內存中的 (Server Side State)

Session用來存儲user的數據   Application用來存儲Application的數據

優缺點與注意事項

易於存儲少量數據非常方便簡單。但需要注意不要存儲敏感數據,不要存儲過大的數據。它們和前面說的Cookie、Session與Application不同。雖然Cookie也是存儲在客戶端,每次提交都附加在HTTP頭中進行提交,但是它的數據量畢竟不大,起了一個標記的作用。Session和Application都是存儲在服務器端的,不會參與頁面往返過程。隱藏域、ViewState和ControlState始終參與往返,而且序列化和反序列化會消耗一定資源,因此,存儲過大的數據會導致網頁加載過慢,浪費服務器帶寬。

***********************************************************************************************************************************************************************************

var與dynamic的區別

Var:只是省略了變量的類型   初始化變量時少輸入一些字,編譯器會根據右值來推斷出變量的類型

Dynamic: 變量的類型是在運行時決定的

*************************************************************************************************************************************************************************************

Lambda =>   "called 'Go To' "

allow functions to be used as data such as variables or fields.

http://www.dotnetperls.com/lambda

**************************************************************************************************************************************************************************************

Global.asax is a file that resides in the root directory of your application

It is a collection of event handlers that you can use to change and set settings in your site.

所包含的事件:

Application_Start, 
Application_End, 
Application_AcquireRequestState, 
Application_AuthenticateRequest, 
Application_AuthorizeRequest, 
Application_BeginRequest, 
Application_Disposed, 
Application_EndRequest, 
Application_Error, 
Application_PostRequestHandlerExecute, 
Application_PreRequestHandlerExecute, 
Application_PreSendRequestContent, 
Application_PreSendRequestHeaders, 
Application_ReleaseRequestState, 
Application_ResolveRequestCache, 
Application_UpdateRequestCache, 
Session_Start, 
Session_End 

****************************************************************************************************

Response.Output.Write() allows you to write formatted output.   Response.Write() without format

*****************************************************************************************************

Turn off the Cookie: Cookie.Discard

Turn off the Session: Session.Abandon

****************************************************************************************************

Static Method can not access the Non-Static method or field 

靜態方法中不能訪問非靜態的方法或字段

靜態類不能用於List<T> 中

*****************************************************************************************************************

List中 Any() 和 All() 的用法

List<int> numbers = new List<int> { 1, 2 };
                bool hasElements = numbers.Any();

                Console.WriteLine("The list {0} empty.",
                    hasElements ? "is not" : "is");

 class Pet
            {
                public string Name { get; set; }
                public int Age { get; set; }
            }

            public static void AllEx()
            {
                // Create an array of Pets.
                Pet[] pets = { new Pet { Name="Barley", Age=10 },
                               new Pet { Name="Boots", Age=4 },
                               new Pet { Name="Whiskers", Age=6 } };

                // Determine whether all pet names 
                // in the array start with 'B'.
                bool allStartWithB = pets.All(pet =>
                                                  pet.Name.StartsWith("B"));

                Console.WriteLine(
                    "{0} pet names start with 'B'.",
                    allStartWithB ? "All" : "Not all");
            }

            // This code produces the following output:
            //
            //  Not all pet names start with 'B'. 

****************************************************************************************************************************************************************************************

ReadOnly 與 Conest 的區別

Private readonly int  INC;

Private Conest int INC = 3;

ReadOnly和Conest 都是用於設置一個值 使其不能被改變

區別是 Conest 需要進行初始化賦值

Readonly 不需要進行初始化, 而且如果程序試圖改變值 錯誤將在編譯階段被報錯

****************************************************************************************************************************************************************************************

類中 static成員的做用

在一個類中 靜態成員的值 可以在所有實例化的對象中共享

例如 一個類 timer 中 有個 靜態成員 private static int hours;

現在又若干個object ----->   timer t1 = new timer();      timer t2 = new timer();

如果t1中 將 hours 設置爲3

則在t2中 hours也是3

其餘的不是靜態變量的成員 每次實例化後 值將會改變

****************************************************************************************************************************************************************************************

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章