Note on <C# 3.0 UNLEASHED With the .NET Framework 3.5> - 02

50 篇文章 0 订阅
15 篇文章 0 订阅

Chapter 16: Declaring Attributes and Examing Code with Reflection


In order to execute the code in listing 16.8: Reflecting.cs, you may just create a new Console Application in VS 2012.

Specify the app name as Reflecting(this is required to run it successfully), and whatever name for the solution:


Remove the default Program.cs file, and add two new .cs files, copy the code from Reflected.cs and Reflecting.cs into them respectively, don't have to bother with namespace Reflecting, which is created by the IDE automatically.

For the sake of convenience, you could copy the code from the samples attached to the book, but remember to change the call to LoadFrom asAssembly.LoadFrom("Reflecting.exe"). And the last thing is that there is an obvious typo in Reflected.cs:

	public object this[int index]
	{
		get
		{
			if (index <= index)
			{
				return myArray[index];
			}
			else
			{
				return null;
			}
		}
		set
		{
			myArray.Add(value);
		}
	}


Change the second index in the if clause to myArray.Count

The result is almost the same as the book, except the order of the items printed on screen:




Chapter 19: Accessing Data with LINQ


LINQ to Objects


Be aware that the actual query doesn’t execute until the foreach loop executes. This allows you to define the query in a number of steps without incurring the overhead of reevaluation upon each change, which increases efficiency.







LINQ to SQL


Whenever you declare a partial method for any of the partial methods that were automatically generated, LINQ to SQL calls your method instead of generating runtime queries. This means that you will be responsible for all operations.


Standard Query Operators


The confusion could come from the fact that you are operating on custom reference type objects—instances of the StaffMember class. As you may recall from Chapter 8, “Designing Objects,” reference type objects default to reference equality. Built-in types already have their equality defined, but you need to define equality for your custom types if you are going to use them in a scenario such as this. The two StaffMember instances of Chris refer to different instances, and reference equality isn’t sufficient.

To solve this problem, you need to use the Distinct operator overload that accepts an IEqualityComparer. Here’s the correct implementation for using the Distinct operator on collections of custom types:


OperatorC# AliasDescription
   
   
   
Filtering Operators
OfTypeN/AObjects of the specified type
WhereWhereObjects meeting specified predicate
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   





Chapter 20: Managing Data with ADO.NET




A Data Reader is a fast-forward streaming object for reading data. It is the fastest way to read information from a database. To write to a database, you need to use a Command object for insert, update, and delete operations.

The DataSet is an in-memory snapshot of selected data. It uses a Data Adapter to read and write data to and from the database. The Data Adapter has four Command object properties for select, insert, update, and delete operations.

As you can see from Figure 20.1, all ADO.NET objects use one or more Command objects, and you can use a Command object directly from your application, too. The Command object holds the query that will be sent to a data source.



Connected and Disconnected Modes


The connected mode of operation means that you must write code that explicitly opens and closes a Connection object. The ADO.NET objects involved in connected mode operations include the Data Reader and Command. You use the Data Reader to select data and the Command to insert, update, and delete.

Disconnected mode means that you can work with data in memory when you don’t have an open connection. The DataSet and Data Adapter objects enable disconnected mode operations. Data Adapters are instrumental for disconnected mode by managing the connection. When retrieving data from the database, the Data Adapter opens a connection, fills the specified DataSet, and then automatically closes the connection as soon as the data is read. After you’ve made changes to the data, you use the Data Adapter again to open the connection, persist changes (whether they are add, update, or delete), and automatically close the connection.

Developers are often concerned about the practicality of the disconnected mode because there is a time between filling a DataSet and updating its data (persisting to the database) where the actual data in the database could have changed. Remember, that although ADO.NET makes it easy to manage data in a disconnected mode, it isn’t the recommended solution for all problems.


string connStr = "Data Source=CHICAGO; Initial Catalog=Hospital; Integrated Security=True";
string queryStr = "select * from Patient";


using (var conn = new SqlConnection(connStr))
using (var cmd = new SqlCommand(queryStr, conn))
{
conn.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
Console.WriteLine("Name: {0}", rdr["Name"]);
}
}
}


The point to make about the preceding code is the using statements. They ensure that Dispose is called, meaning that critical resources get released in a timely manner.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值