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

其余的不是静态变量的成员 每次实例化后 值将会改变

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

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